"; $a0[0] = "hamburger"; // Note the odd behavior here $a0[1] = "pizza"; $a0[2] = "hotdog"; var_dump ($a0); echo "

"; // $a0 is a string, assigning a value to a certain position of a string // causes a replacement to the character unset($a0); // set $a0 back to null // Let's assign a value to a certain position of an array $a0[0] = "hamburger"; // This time array indexing works correctly $a0[1] = "hotdog"; $a0[2] = "pizza"; var_dump ($a0); echo "

"; echo "Several ways to create arrays
"; // Creating an array $list[0] = 17; $list[1] = "Today is my birthday"; $list[] = 42; var_dump($list); echo "
"; // An array can also be created using an array constructor $list = array(17, 24, "45", 91); var_dump($list); echo "
"; $list[8] = "skip index"; var_dump($list); echo "

"; // Another way to create an array $ages = array("Joe" => 42, "Mary" => 41, "John" => 71); var_dump($ages); echo "
"; $ages['Tim'] = 29; var_dump($ages); echo "

"; echo "Access array_keys and array_values
"; $name = array_keys($ages); $age = array_values($ages); if (sizeof($name) > 0) if (array_key_exists("Mary", $ages)) echo "A key \"Mary\" exists in an array \$ages
"; else echo "A key \"Mary\" does not exist in an array \$ages
"; // Functions we can use to access an element in an array // current() - returns the value of the current element in an array // end() - moves the internal pointer to, and outputs, the last element in the array // next() - moves the internal pointer to, and outputs, the next element in the array // prev() - moves the internal pointer to, and outputs, the previous element in the array // each() - returns the current element key and value, and moves the internal pointer forward // reset() -- reset the internal pointer to the first element in the array // let's iterate the array $curr = current($ages); while ($curr): echo $curr ."
"; $curr = next($ages); // point to the next item in an array // notice that the value is displayed endwhile; // let's iterate the array_keys $curr = current($name); while ($curr): echo $curr ."
"; $curr = next($name); // point to the next item in an array_key // array $name builds index of each key endwhile; // Once we have iterated on the array, to do so again we need to reset // it first, as shown below. reset($ages); while ($curr = each($ages)): $k = $curr["key"]; $v = $curr["value"]; echo "key is $k and value is $v
"; endwhile; // Use the ksort() function to sort by keys ksort($ages); // echo $ages; // what happen if we echo an array ? // There are several ways to display information. // var_dump() function prints everything about a variable (type, size, value) // print_r() function prints human-readable information about a variable. print_r($ages); echo "
"; // Use sort() function to sort by values sort($ages); print_r($ages); echo "
"; ?>

sort and ksort example

31, "Al" => 27, "Gandalf" => "wizard", "Betty" => 42, "Frodo" => "hobbit"); echo "Original array
"; // print original array foreach ($original as $key => $value) print("[$key] => $value
"); $new = $original; sort($new); echo "
Sorted array (using sort() function)
"; foreach ($new as $key => $value) // syntax: foreach (array as key => value) then loop body print("[$key] => $value
"); $new = $original; ksort($new); echo "
Sorted array (using ksort() function)
"; foreach ($new as $key => $value) print("[$key] => $value
"); ?>