0

I have two arrays,

Array one:

$amount = array(0 => 11, 1 => 22, 2 => 33);

Array two:

$id = array(1 => 1);

I would like to get a result like this, by comparing:

result = array(1 => 22);

I've been trying to do this with several PHP functions, but my brain has now melted.

Hope there is some help out there.

3 Answers 3

2

Though your question is bit vague I guess what you need is array_intersect_key,

$common = array_intersect_key($amount,$id);

DEMO.

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

1 Comment

Thank you. Id been using array_intersect() before, but thanks :-)
0

If I understand you correctly, you want to loop through the keys in $id and get the corresponding $amount into an array?

$result = array();
foreach($id as $key => $value) {
    if(isset($amount[$key])) {
        $result[$key] = $amount[$key];
    }
    else {
        // handle situation where
        // $amount doesn't have that key
    }
}

Comments

0
$array=array();
 foreach($id as $key=>$val){
 if(array_key_exists($key,$amount)){
   $array[$key] = $amount[$key];
 };
}

print_r($array);

DEMO

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.