note 42045 deleted from language.types.array by tomsommer

From: Date: Sat, 03 Jul 2004 13:15:47 +0000
Subject: note 42045 deleted from language.types.array by tomsommer
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-72417@lists.php.net to get a copy of this message
Note Submitter: yevbo23 at yahoo dot com ---- 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.

« previous php.notes (#72417) next »