Learning PHP - Part 3 - Arrays, Arrays, and more Arrays

Part 3

Laracasts main site
Laracasts - PHP for beginners

Chapters covered:

Chapter 6 - Understanding Arrays
Chapter 7 - Associative Arrays

Chapter 6 - Understanding Arrays

Looping through an array

Php
// index.php
<?php

$names = ['Bob', 'Billy', 'Jimmy'];

foreach ($names as $name) {
  echo $name . ', ';
};

// Outputs: Bob, Billy, Jimmy,


Alright, now lets break it out to the view and create a list:

Php
// index.php
<?php

$names = ['Bob', 'Billy', 'Jimmy'];
Php
// index.view.php
<ul>
  <?php
    foreach ($names as $name) {
      echo "<li>$name</li>";
    }
  ?>

  <-- Alternative syntax -->
  <?php foreach ($names as $name) : ?>
    <!-- PHP parsing has stopped, drop to HTML -->
    <li>
      <!-- Start parsing PHP -->
      <?= $name ?>
      <!-- End parsing PHP -->
    </li>
  <!-- End the loop by parsing PHP again -->
  <?php endforeach; ?>
</ul>


This will produce the following:


  • bob
  • billy
  • jimmy
  • bob
  • billy
  • jimmy

Chapter 7 - Associative Arrays

These appear to me to be similar to a ruby hash so here we go:

Php
// index.php
<?php

$person = [
  'age' => 23,
  'hair' => 'blonde',
  'career' => 'web developer'
];

require 'index.view.php';
Php
// index.view.php
<!-- Above html omitted for brevity -->
<ul>
  <?php foreach ($person as $key => $feature) : ?>
    <li>
      <strong><?= $key; ?>: </strong><?= $feature; ?>
    </li>
  <?php endforeach; ?>
</ul>
<!-- Below html omitted for brevity -->

This will produce the following:


  • age: 23
  • hair: blonde
  • career: web developer

Pushing to Arrays

Php
// index.php
<?php

// Pushing to associative arrays
$person = [
  'age' => 23,
  'hair' => 'blonde',
  'career' => 'web developer'
];

$person['name'] = 'Bob';
// Appends bob to $person

// Pushing to non-associative arrays
$animals = ['dog', 'cat'];

$animals[] = 'zebra';
// Appends 'zebra' to $animals

Printing Arrays

Php
// index.php
<?php

$person = [
  'age' => 23,
  'hair' => 'blonde',
  'career' => 'web developer'
];

$person['name'] = 'Bob';

// Will convert the array to a string then print it
var_dump($person);

// Will stop parsing after this function, will still print $person
die(var_dump($person));

// Wont get evaluated because of die();
require 'index.view.php'

Removing an item from an associative array

Php
// index.php
<?php

// associative array
$person = [
  'age' => 23,
  'hair' => 'blonde',
  'career' => 'web developer'
];

// Remove hair
unset($person['hair']);

/* $person = [
    'age' => 23,
    'career' => 'web developer'
    ]
*/

Homework

Php
// homework.php
<?php

$task = [
  'title' => 'Renew registration',
  'due_date' => 'tomorrow',
  'assigned_to' => 'Konnor',
  'completed' => True
];

Follow along with my repo
Laracasts main site
PHP for beginners