Free Training
C Language   |   CSS   |   MainFrame   |   VBScript   |   PHP   |   XML   |   C++ Tutorials   |   Ajax   |   JavaScript   |   CSS3   |   UML   |   jQuery   |   Microsoft AJAX

Sponsored Links

PHP Tutorials

 
Home Tutorials PHP
 

PHP Tutorials - Arrays (Part 2)

 
Category: PHP
Comments (1)

PHP Tutorials - Arrays (Part 2)

In this PHP Tutorial you will learn Arrays (Part 2) - Looping with foreach( ), Sorting arrays and Using Multidimensional Arrays.



Looping with foreach( ):

The easiest way to iterate through each element of an array is with foreach( ).
The foreach( ) construct lets you run a code block once for each element in an array.


Example:


$fruits = array(‘apple’, ‘banana’, ‘orange’);
print “<table>\n”;
foreach ($fruits as $fruit) {
print “<tr><td>$fruit</td></tr>\n”;
}
print ‘</table>’;


This will print:


<table>
<tr><td>apple</td></tr>
<tr><td>banana</td></tr>
<tr><td>orange</td></tr>
</table>


if you want to know what element you're on as you're iterating through a numeric array, use for( ) instead of foreach( ).


Your for( ) loop should depend on a loop variable that starts at 0 and continues up to one less than the number of elements in the array.


Example:


$fruits = array(‘apple’, ‘banana’, ‘orange’);
$num = count($fruits);
for ($i = 0, $i < $num; $i++) {
print "Fruit number $i is $fruits[$i]\n";
}


This will print:


Fruit number 0 is apple
Fruit number 0 is banana
Fruit number 0 is orange


Sorting arrays:

To sort array values, you can use the function sort().


Example:


$dinner = array('Sweet Corn and Asparagus',
'Lemon Chicken',
'Braised Bamboo Fungus');
$meal = array('breakfast' => 'Walnut Bun',
'lunch' => 'Cashew Nuts and White Mushrooms',
'snack' => 'Dried Mulberries',
'dinner' => 'Eggplant with Chili Sauce');
sort($dinner);
sort($meal);
foreach ($dinner as $key => $value) {
print "\$dinner: $key $value\n";
}
foreach ($meal as $key => $value) {
print " \$meal: $key $value\n";
}


This will print:


$dinner: 0 Braised Bamboo Fungus
$dinner: 1 Lemon Chicken
$dinner: 2 Sweet Corn and Asparagus
$meal: 0 Cashew Nuts and White Mushrooms
$meal: 1 Dried Mulberries
$meal: 2 Eggplant with Chili Sauce
$meal: 3 Walnut Bun


can also sort arrays by key with ksort( ). This keeps key/value pairs together, but orders them by key.


Example:


$meal = array('breakfast' => 'Walnut Bun',
'lunch' => 'Cashew Nuts and White Mushrooms',
'snack' => 'Dried Mulberries',
'dinner' => 'Eggplant with Chili Sauce');
ksort($meal);
foreach ($meal as $key => $value) {
print "\$meal: $key $value\n";
}


This will print:


$meal: breakfast Walnut Bun
$meal: dinner Eggplant with Chili Sauce
$meal: lunch Cashew Nuts and White Mushrooms
$meal: snack Dried Mulberries


Using Multidimensional Arrays:

Use the array( ) construct to create arrays that have more arrays as element values.



Example:


$lunches = array( array('Chicken','Eggplant','Rice'),
array('Beef','Scallions','Noodles'),
array('Eggplant','Tofu'));
print $lunches[0][0];
print $lunches[2][1];


This will print:


Chicken
Tofu



Read Next: PHP Tutorials – File manipulation (Part 1)



 

 

Comments


heena said:

  Fruit number 0 is apple
Fruit number 0 is banana
Fruit number 0 is orange

Above result is wrong

The loop will give following result

Fruit number 0 is apple
Fruit number 1 is banana
Fruit number 2 is orange

Please correct it....


September 20, 2007, 8:32 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links