elefant Posted April 20, 2008 Share Posted April 20, 2008 I can't figure the logic of this out... The number of functions for dealing with arrays is just overwhelming. The scenario is this. The first multi-d associative... $item_list = array( someitem1 => array( name => "Some Item", desc => "Description of the item", price => '$$.cc' ), And in another multi-d associative... $customer_list = array( someperson => array( fname => "FIRST", lname => "LAST", more => "more stuff", favoriteitem => 'someitem1' ), So, in the second multi-d associative array there may be more than one occurence of someitem1 being the favorite item... And what I want to do is generate a list where for every instance of someitem1 I can display more info about that person... But I don't want to see info about people that don't have someitem1 as their favorite item. Sorry if this is stupid question :'( Not even sure if you needed to know about the first array... Link to comment https://forums.phpfreaks.com/topic/101959-solved-get-multiple-values-from-array-based-on-somevalue/ Share on other sites More sharing options...
Barand Posted April 20, 2008 Share Posted April 20, 2008 try <?php $customer_list = array( 'someperson1' => array( 'fname' => "FIRST", 'lname' => "LAST", 'more' => "more stuff", 'favoriteitem' => 'someitem1' ), 'someperson2' => array( 'fname' => "FIRST", 'lname' => "LAST", 'more' => "more stuff", 'favoriteitem' => 'someitem2' ), 'someperson3' => array( 'fname' => "FIRST", 'lname' => "LAST", 'more' => "more stuff", 'favoriteitem' => 'someitem1' ) ); $search_item = 'someitem1'; $selected = array(); foreach ($customer_list as $k => $cdata) { if ($cdata['favoriteitem'] == $search_item) $selected[$k] = $cdata; } echo '<pre>', print_r($selected, true), '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/101959-solved-get-multiple-values-from-array-based-on-somevalue/#findComment-521801 Share on other sites More sharing options...
elefant Posted April 20, 2008 Author Share Posted April 20, 2008 Awesome! One last thing, though. How do I pull out specific parts of the customers_list vs displaying the whole array? Thanks. Link to comment https://forums.phpfreaks.com/topic/101959-solved-get-multiple-values-from-array-based-on-somevalue/#findComment-522123 Share on other sites More sharing options...
Barand Posted April 20, 2008 Share Posted April 20, 2008 Something like <?php foreach ($selected as $k => $cdata) { echo "$k {$cdata['fname']} {$cdata['lname']} <br>"; } Link to comment https://forums.phpfreaks.com/topic/101959-solved-get-multiple-values-from-array-based-on-somevalue/#findComment-522172 Share on other sites More sharing options...
elefant Posted April 20, 2008 Author Share Posted April 20, 2008 *Bows* Thank you! Link to comment https://forums.phpfreaks.com/topic/101959-solved-get-multiple-values-from-array-based-on-somevalue/#findComment-522207 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.