Associative Array

On Day 5, we learned about Array. Today we will learn some advanced Arrays.



day_6_array_1.php

PHP Code:
<?php

$iceCream 
= array();

$iceCream['name'] = 'Fudge Brownie Ice Cream';
$iceCream['price'] =  50;
$iceCream['available'] = 'yes';
$iceCream['supplier'] = 'Uncle John';

// Lets see what is in the array.

echo '<pre>';
print_r($iceCream);
echo 
'</pre>';

// To print value stored in associative array, we use names instead of number.

echo '<h1>Product Details</h1>';
echo 
'<b>Name:</b> '  $iceCream['name']  . '<br>';
echo 
'<b>Price:</b> Rs: '  $iceCream['price']  . '/-<br>';
echo 
'<b>Supplier:</b> '  $iceCream['supplier']  . '<br>';
echo 
'<b>In Stock:</b> '  $iceCream['available']  . '<br>';
The the code

Code:
$iceCream = array();
creates an empty array.


Multi Dimensional Array

You can store an array inside an array. For example, in previous example, we have an array that store information about an ice cream. What if we need to store information about more than one ice cream ?

$iceCream variable store information about one ice cream. Lets create another variable $iceCreams (plural form for $iceCream, notice "s" at the end of variable name).

day_6_array_2.php

PHP Code:
<?php

$iceCreams 
= array();

$iceCream1 = array();

$iceCream1['name'] = 'Fudge Brownie Ice Cream';
$iceCream1['price'] =  50;
$iceCream1['available'] = 'yes';
$iceCream1['supplier'] = 'Uncle John';

$iceCream2 = array();

$iceCream2['name'] = 'Chocolate Ice Cream';
$iceCream2['price'] =  40;
$iceCream2['available'] = 'yes';
$iceCream2['supplier'] = 'Amul';

$iceCream3 = array();

$iceCream3['name'] = 'Movenpick Ice Cream';
$iceCream3['price'] =  80;
$iceCream3['available'] = 'no';
$iceCream3['supplier'] = 'Dairy Queen';


$iceCreams[] = $iceCream1;
$iceCreams[] = $iceCream2;
$iceCreams[] = $iceCream3;

/*

// Lets print_r() the array

echo '<pre>';
print_r($iceCreams);
echo '</pre>';

*/

// Lets list icecreams more user friendly way using foreach

echo '<h1>Menu</h1>';

foreach (
$iceCreams as $iceCream) {
    echo 
'<hr>';
    echo 
'<b>Name:</b> '  $iceCream['name']  . '<br>';
    echo 
'<b>Price:</b> Rs: '  $iceCream['price']  . '/-<br>';
    echo 
'<b>Supplier:</b> '  $iceCream['supplier']  . '<br>';
    echo 
'<b>In Stock:</b> '  $iceCream['available']  . '<br>';

    if (
$iceCream['available'] == 'yes') {
        echo 
'<p>Product in stock, buy it before it melt away.</p>';
    }
}

echo 
'<hr>'// <hr> draw a line in your browser, thanks to HTML.

/* and */ are multi line comment in PHP. Anything side that won't get executed. If you want to see raw data stored in array, find

Code:
/*

// Lets print_r() the array

echo '<pre>';
print_r($iceCreams);
echo '</pre>';

*/
Replace it with

Code:
// Lets print_r() the array

echo '<pre>';
print_r($iceCreams);
echo '</pre>';
It is very important to understand use of associative array. It is not that complicated. We don't do complex codes, we keep our codes simple, so it is easily maintainable.

Exercise

  1. Use strtolower() function to ice cream name to lower case.
  2. Use strtoupper() function to change ice cream name to upper case.