Giddy Rob Posted May 27, 2008 Share Posted May 27, 2008 Hey, I have done a function to find a value in a multidemensional array but was wondering how I find out what the main index for the record containing the value? i.e. array item[0] > [0]=34, [1]=curtains, [2]=£2.99 item[1] > [0]=58, [1]=tent, [2]=£8.99 item[2] > [0]=69, [1]=ruler, [2]=£1.99 I can find the value say 58 by using this code to pass the session array and the id number which is stored somewhere in the array: function findrecord($session, $id){ foreach($session as $key => $file){ if($file[0] == $id){ return $file[0]; } } } the problem is I need to be able to tell that it is coming from item[1] for my system to work as I want to update it and need to know which one 58 is in the array. Hope that makes sense. Cheers Rob Quote Link to comment https://forums.phpfreaks.com/topic/107446-identify-index-of-multidimensional-array/ Share on other sites More sharing options...
Giddy Rob Posted May 27, 2008 Author Share Posted May 27, 2008 item[0] > [0]=34, [1]=curtains, [2]=£2.99 item[1] > [0]=58, [1]=tent, [2]=£8.99 item[2] > [0]=69, [1]=ruler, [2]=£1.99 Quote Link to comment https://forums.phpfreaks.com/topic/107446-identify-index-of-multidimensional-array/#findComment-550737 Share on other sites More sharing options...
sasa Posted May 27, 2008 Share Posted May 27, 2008 try <?php function findrecord($session, $id){ foreach($session as $key => $file){ if($file[0] == $id){ return $key; } } } $item = array( array(34, 'curtain', '£2.99'), array(58, 'tent', '£8.99'), array(69, 'ruler', '£1.99') ); $a = findrecord($item, 58); echo $a; ?> Quote Link to comment https://forums.phpfreaks.com/topic/107446-identify-index-of-multidimensional-array/#findComment-550781 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.