msaz87 Posted March 28, 2010 Share Posted March 28, 2010 Hey all, I have two related arrays, one of which I'm sorting and what I'm wondering is if you can search the second array by an index value and returning its value. The first array, $first_array, is sorted so it looks like this: Array ( [5] => 6 [2] => 6 [1] => 2 [6] => 2 [3] => 2 [0] => 2 [4] => 2) And the other array, $second_array, is more like: Array ( [0] => 4 [1] => 2 [2] => 12 [3] => 9 [4] => 7 [5] => 20 [6] => 21) So you could search by index value "5" from the first array and return the value "20" from the second. Something like: foreach($first_array as $key => $value){ echo "FIRST VALUE: ".$value; $second_value = XXsearch_arrayXX($second_array,$key); echo "SECOND VALUE: ".$second_value; } Is this possible? Any help is greatly appreciated -- thanks! Quote Link to comment https://forums.phpfreaks.com/topic/196815-search-array-for-index-and-return-value/ Share on other sites More sharing options...
premiso Posted March 28, 2010 Share Posted March 28, 2010 mmmm array_search Sometimes reading the manual can help. Edit: But if the value is just the index, there is not even a need to do this would suffice: if (isset($second_array[$key])) { $second_value = $second_array[$key]; } Simple as that. Quote Link to comment https://forums.phpfreaks.com/topic/196815-search-array-for-index-and-return-value/#findComment-1033177 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.