PHP arrays
What is an array?
Logically you can think of an array as a variable that holds multiple instances of other variables in a way where they can be indexed using rows and columns or even more dimensions.
Physically, they are little blocks of memory that are logically mapped to each other like a mini database.
For example
<?php
$our_array = array(); //defines an empty array
$array2 = array(20,40); //defines an array with 20 at the index 0 and 40 at the index 1
echo $array2[1]; //we can’t echo $our_array as it doesn’t contain anything and the PHP //interpreter will throw an error
?>
Will output: 40
Well that’s all fine and good if we want to only use two dimensional arrays with the default of indexes. But what if we don’t want to go by the default numerical index?
<?php
$car = array(“make” =--> “car_maker”,
“model”=>”car_model”);
echo $car[‘make’].”< br/>”;
echo $car[‘model];
?>
outputs
car_maker
car_model
Now isn’t that a much easier way to name our indexes?
Adding values
What about adding values to our simple arrays?
Adding a value is very simple!
<?php
$array = [“bob”,”frank”,”tom”];//new syntax as of php 5.4 is the same as array()
$array[] = “fred”;
echo $array[3];
?>
This will output: fred.
Deleting values or deleting the array
First lets look at deleting our array. This is easily done with the unset() function.
php delete element
<?php
$array = [“some_records”];
unset($array);
// Deleting an element of the array is just as easy.
$array = [“some_record”=-->”value”,
“another” =>”itsvalue”];
unset($array[‘some_record’]);
?>
Printing the values of the array
Now if you get into multidimensional arrays from time to time you need to look at the array structure in a human readable form for debugging purposes. Using the html <pre> </pre> tags we can print a nice formatted array of multi dimensional one.
<?php $items = array("hat" =--> array("price" => 3.5,
"description" => "A real nice yellow hat",
"sizes" => "one size fits all",
"id" => "hat"),
"shirt" => array("price" => 16.95,
"description" => "A purple cotton shirt",
"sizes" => "M, L, XL",
"id" => "shirt"),
"pants" => array("price" => 45,
"description" => "Blue pants that you can wear",
"sizes" => "30 to 46 w and 28 to 42 l",
"id" => "pants"),
"shoes" => array("price" => 20.95,
"description" => "Yellow shoes with green laces",
"sizes" => "6 to 14 mens",
"id" => "shoes"));
// now lets print it so we can take a look
echo “<br />”;
print_r($items);
echo “<br />”;
?>
This outputs
Array ( [hat] => Array ( [price] => 3.5 [description] => A real nice yellow hat [sizes] => one size fits all [id] => hat ) [shirt] => Array ( [price] => 16.95 [description] => A purple cotton shirt [sizes] => M, L, XL [id] => shirt ) [pants] => Array ( [price] => 45 [description] => Blue pants that you can wear [sizes] => 30 to 46 w and 28 to 42 l [id] => pants ) [shoes] => Array ( [price] => 20.95 [description] => Yellow shoes with green laces [sizes] => 6 to 14 mens [id] => shoes ) )
HINT: to access values in a mutidimensional array use this syntax
to get the price of the hat $items[‘hat’][‘price’]
Accessing each value in a loop without knowing the size of the array with the php foreach() function.
…to be continued in part 4