note 42045 added to language.types.array
| From: | yevbo23atyahoodotcom at rn2 dot php dot net | Date: | Fri, 30 Apr 2004 16:15:59 +0000 |
| Subject: | note 42045 added to language.types.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-69043@lists.php.net to get a copy of this message | ||
Here is a trick I couldn't find in books. Say we have this nested array:
$arr = array();
$arr[] = array('term' => 'apple',
'definition' => 'fruit');
$arr[] = array('term' => 'cucumber',
'definition' => 'vegetable');
$arr[] = array('term' => 'banana',
'definition' => 'fruit');
how do we find the values of all terms? You cannot do this by referencing say, 'apple'
term as $arr[0]['term']. The way to do is by simply typing $arr['term'] in
foreach loop - yes, PHP will go inside the nested arrays, checking for 'term' values!
Example:
foreach ($arr as $item) {
echo $item['term'].'=>'.$item['definition'].'<br
/>';
}
will return:
apple=>fruit
cucumber=>vegetable
banana=>fruit
as intended.
----
Manual Page -- http://www.php.net/manual/en/language.types.array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42045
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42045&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42045&report=yes
Search -- http://master.php.net/manage/user-notes.php