FartMachine4U
FartMachine4U

Reputation: 115

Finding if array values in one array exist in another, and if so, at which key location

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.

Upvotes: 0

Views: 262

Answers (4)

garoevans
garoevans

Reputation: 155

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

Upvotes: 4

Slavenko Miljic
Slavenko Miljic

Reputation: 3856

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

Upvotes: 2

deW1
deW1

Reputation: 5660

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

Upvotes: 3

Jompper
Jompper

Reputation: 1402

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

Upvotes: 2

Related Questions