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.

Tasos K.
  • 7,979
  • 7
  • 39
  • 63
FartMachine4U
  • 117
  • 1
  • 3
  • 10
  • 2
    See if this answer helps http://stackoverflow.com/a/8881719/ and http://www.php.net/in_array – Funk Forty Niner Jan 22 '14 at 21:43
  • No answer to the issue of selecting a member based on its largest key or a largest position. – digitai Jan 22 '14 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. – Fleshgrinder Jan 22 '14 at 22:01

4 Answers4

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).

Fleshgrinder
  • 15,703
  • 4
  • 47
  • 56
garoevans
  • 155
  • 4
  • 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. – FartMachine4U Jan 23 '14 at 04:04
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

Community
  • 1
  • 1
deW1
  • 5,562
  • 10
  • 38
  • 54
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)
Jompper
  • 1,412
  • 9
  • 15
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);
Slavenko Miljic
  • 3,836
  • 2
  • 23
  • 36