Jump to content

Search array for index and return value?


msaz87

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/196815-search-array-for-index-and-return-value/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.