iversonm Posted October 9, 2008 Share Posted October 9, 2008 so far i have $array = array(0 => array('blue', 1), 1 => array('red', 2), 2 => array('green', 3), 3 => array('red', 4)); $key = array_search('green', $array); // i want $key=2 echo $key;//should echo 2 but it echos nothing how do i get this to work, Quote Link to comment Share on other sites More sharing options...
R0bb0b Posted October 9, 2008 Share Posted October 9, 2008 I think you will have to loop through each array and search them individually. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 9, 2008 Share Posted October 9, 2008 That's because you have a multidimensional array. Just create a custom search function since you are using a custom array function search_myarray($search_val, $input_array) { $return_val = false; foreach($input_array as $key => $sub_array) { if ($sub_array[0]==$search_val) { $return_val = $key; break; } } return $return_val; } $array = array( 0 => array('blue', 1), 1 => array('red', 2), 2 => array('green', 3), 3 => array('red', 4) ); $key = search_myarray('green', $array); echo $key; //Will echo 2 Quote Link to comment Share on other sites More sharing options...
iversonm Posted October 9, 2008 Author Share Posted October 9, 2008 hrm.... let see if i had to scale how well that worked from 1 to 10 it would most definaitly be a 12 so i thank you, it does exactly what i want it to thankyou mjdamato topic solved, im sure you all will you will all see me again Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 9, 2008 Share Posted October 9, 2008 I would add... There might be a reason why you are contructing your array in that manner, but unless the the key of the first array value and the numeric value in the secondary array are unique values that you really need (e.g. 2 & 3 for green), I would suggest creating your array like this: $array = array( 1 => 'blue', 2 => 'red', 3 => 'green', 4 => 'red' ); Quote Link to comment Share on other sites More sharing options...
iversonm Posted October 10, 2008 Author Share Posted October 10, 2008 well i just added the numbers in to show that it was array inside an array and if the second array only had 1 object then it would be pointless to have a second array but inside each second array is about 50 variables that all need to be accounted for sooo i couldn't just use array array_multisort like before but thanks for the input 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.