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 Quote Link to comment 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); ?> Quote Link to comment Share on other sites More sharing options...
poe Posted November 7, 2007 Author Share Posted November 7, 2007 thanks Quote Link to comment 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.