damian
damian

Reputation: 55

search array in array and return key

I have two arrays

1.$ids;

Array
(
    [0] => 2427975642
    [1] => 2397521678
)

2.$c

Array
(
    [48] => 2397521678
    [46] => 461
    [45] => 451
)

Question: Search values from $ids in $c and return new array with id. Example return 48

Upvotes: 1

Views: 151

Answers (4)

Rupesh Patel
Rupesh Patel

Reputation: 3065

    $ids= array
    (
        [0] => 2427975642
        [1] => 2397521678
    );

    $c =array
    (
        [48] => 2397521678
        [46] => 461
        [45] => 451
    );

$res = array_intersect($ids,$c);
$keys = array_keys($res);
print_r($keys);

Upvotes: 1

user849137
user849137

Reputation:

$ids = array(   
    2427975642,
    2397521678
);



$c = array(
    48 => 2397521678,
    46 => 461,
    45 => 451
);

$finalArray = array();

foreach ( $c as $key=>$val)
{

if ( array_search($val,$ids))
{

$finalArray[]=$key;

}

}

Upvotes: 0

Erwin Moller
Erwin Moller

Reputation: 2408

$ids = array(   
    2427975642,
    2397521678
);



$c = array(
    48 => 2397521678,
    46 => 461,
    45 => 451
);

$common = array_keys(array_intersect($c, $ids));

print_r($common);

Upvotes: 1

Karl Moodh
Karl Moodh

Reputation: 2711

Check out the function array_intersect: http://php.net/manual/en/function.array-intersect.php

Upvotes: 1

Related Questions