poe Posted November 7, 2007 Share Posted November 7, 2007 i have an array: Array ( [0] => Array ( [planid] => 1 [price] => 10 [term] => 1 ) [1] => Array ( [planid] => 3 [price] => 25 [term] => 2 ) [2] => Array ( [planid] => 7 [price] => 50 [term] => 3 ) ) how do i search the 'planid' values and return the matching 'price' value ie search planid = 7, the return would be $myarray['price'] = 50 or if i search on planid and want matching term. ie search planid =3, $myarray['term'] = 2 Link to comment https://forums.phpfreaks.com/topic/76422-solved-search-in-an-array/ Share on other sites More sharing options...
obsidian Posted November 7, 2007 Share Posted November 7, 2007 This is where a foreach loop will really be helpful: <?php // Assume $records holds your array declared above function searchPlans($records, $s, $val) { foreach ($records as $r) { if ($r['planid'] == $val) { return $r[$s]; } } } $id = 1; $price = searchPlans($records, 'price', $id); $term = searchPlans($records, 'term', $id); ?> Link to comment https://forums.phpfreaks.com/topic/76422-solved-search-in-an-array/#findComment-387016 Share on other sites More sharing options...
poe Posted November 7, 2007 Author Share Posted November 7, 2007 thanks Link to comment https://forums.phpfreaks.com/topic/76422-solved-search-in-an-array/#findComment-387027 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.