Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.