0

Been looking at array_intersect but not sure how to pull the key location/element location of the elements found:

Say:

$a = array('a','b');

$b = array('c','x','b','a','y','z');

I would like to find where element "a" an "b" from array $a exist in array "$b"

I intend then to flag or store the largest element value found (ie. "b" from array $a) in some other variable.

In this case example, 'a' from array $a has a location of "3" in array $b, and 'b' from array $a has a location of '2' in array $b, thus value 'a' is larger than 'b'. I would then want to store the value of 'a' in some other variable.

Appreciate it.

3
  • 2
    See if this answer helps stackoverflow.com/a/8881719 and php.net/in_array Commented Jan 22, 2014 at 21:43
  • No answer to the issue of selecting a member based on its largest key or a largest position. Commented Jan 22, 2014 at 21:59
  • 1
    garoevans answer is selecting the largest key and it should be the fastest solution as well because it's using built in PHP functions to generate it. Of course this depends on the sizes of the arrays because a function call might produce more overhead than looping with very small ones. But it's also the easiest to understand code which increases maintainability. Commented Jan 22, 2014 at 22:01

4 Answers 4

4

You can continue to use array_intersect() but the opposite way around to how you would originally imagine. It maintains the keys, so start with the array who's keys you want to analyze.

<?php

$a = array('a','b');
$b = array('c','x','b','a','y','z');

// array(2) { [2]=> string(1) "b" [3]=> string(1) "a" }
$intersect = array_intersect($b, $a);

// a
echo end($intersect);

You can see from the output of the array intersect that you get the keys of 2 and 3 to work with. All you need to do then is get the highest one (I used end() here).

Sign up to request clarification or add additional context in comments.

1 Comment

I initially looked at array_intersect and swapped the arrays to get their position as you indicated. I thought there would be other ways. I will try this. Thank you all for the support and the suggestions. It is greatly appreciated.
3

You can use: Array Search

<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;
?>

To find the Key of the Element.

Or: in_array

<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
    echo "Got Irix";
}
if (in_array("mac", $os)) {
    echo "Got mac";
}
?>

to just find out if an array contains a certain value.

And in case you're going to do that in Multi Dimensional Arrays:

Search by Key Value in a multidimensional array

Comments

2
<?php
$a = array('a','b');
$b = array('c','x','b','a','y','z');

// Search single
$ai = array_search('a', $b);
$bi = array_search('b', $b);

// Loop all
foreach($a as $k){
    $i = array_search($k, $b);
    if($i !== false){
        // $i contains key
    }
}

// Generate array with indexes
$c = array();
foreach($a as $k){
    $i = array_search($k, $b);
    if($i !== false){
        $c[$k] = $i;
    }
}

// Now c is like this array('a' => 3, 'b' => 2)

Comments

2

I'm sure there are many solutions but you can just loop through the arrays and when the values match, add that key and value into a new array:

$a = array('a','b');

$b = array('c','x','b','a','y','z');

$c = array();

for($i=0;$i<count($a);$i++)
{
    $a_el = $a[$i];

    for($y=0;$y<count($b);$y++)
    {
        $b_el = $b[$y];

        if($a_el == $b_el)
        {
            $c[$y] = $b_el;
        }       
    }   
}

var_dump($c);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.