Cinner Posted May 23, 2007 Share Posted May 23, 2007 I have a multidimensional array that fills with data from a database. The array could look like this: Array ( [0] => Array ( [guests] => Array ( [name] => Barbara Bouchet [id] => 32 [photo] => /bouchet.jpg [bio] => This is her biography. ) ) [1] => Array ( [guests] => Array ( [name] => Edwige Fenech [id] => 3 [photo] => /fenech.jpg [bio] => I'm a great actress. ) ) ) This is how the array is composed: $posts[] = array( 'guests' => array( 'name' => $row['name'], 'id' => $row['guestID'], 'photo' => $row['photourl'], 'bio' => $row['biography'] ), ); Let's say I want to display all the information of a particular guest, but I only know his 'id' (like $guestID = 3). How do I do this? I think I would need to know which array key belongs to that 'id' and them I could just use something like echo $posts[1]['guests']['name'] to display the data, right? Quote Link to comment https://forums.phpfreaks.com/topic/52684-searching-and-displaying-a-record-in-a-multidimensional-array/ Share on other sites More sharing options...
AV1611 Posted May 23, 2007 Share Posted May 23, 2007 Um, can the answer be to change the query building the array? otherwise (I think) you would have to loop through the array and clause out each instance where the [iD] is equal to the ID you are looking for... But I could be wrong... Quote Link to comment https://forums.phpfreaks.com/topic/52684-searching-and-displaying-a-record-in-a-multidimensional-array/#findComment-260107 Share on other sites More sharing options...
per1os Posted May 23, 2007 Share Posted May 23, 2007 <?php function myArraySearch($haystack, $needleName, $needleVal) { if (!is_array($haystack)) { return false; } foreach ($haystack as $key => $val) { if (is_array($haystack[$key])) { if (myArraySearch($haystack[$key], $needleName, $needleVal)) { return $haystack[$key]; } }else { if ($key == $needleName) { if ($val == $needleVal) { return true; } } } } return false; } ?> Maybe that will work? Quote Link to comment https://forums.phpfreaks.com/topic/52684-searching-and-displaying-a-record-in-a-multidimensional-array/#findComment-260163 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.