1

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

3
  • 2
    Please rephrase your question. It's really hard to see what you are asking. Commented Aug 7, 2012 at 8:27
  • Are you trying to combine/merge the two arrays by their values? And the latter keys should overwrite the former ones? Commented Aug 7, 2012 at 8:35
  • Merge eliminated because the resulting table is entry identifiers in the database to update Commented Aug 7, 2012 at 8:42

4 Answers 4

1
$ids = array(   
    2427975642,
    2397521678
);



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

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

print_r($common);
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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

Comments

0
$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;

}

}

1 Comment

$results = array(); foreach($ids as $val) foreach($c as $key=>$val2) if($val == $val2) array_push($results, $key); // or to get just the first, // replace the if statement with // // if($val == $val2) { // $result = $key; // break 2; // } print_array($results);

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.