在php中搜索关联数组中的元素

时间:2017-08-22 06:40:08

标签: php

我有这个数组......

Array ( 
    [0] => 72 
    [1] => 73 
    [2] => 74 
)

我希望在下面给出的多维数组中搜索这些元素..我想与" lead_image_id"进行比较。如上所述,数组实际上是" lead_image_id"

的数组
 Array 
 (
    [0] => stdClass Object (
        [lead_image_id] => 60
        [lead_image_name] => assets/images/logo.png
        [lead_image_lead_id] => 74
        [lead_image_status] => 0
    )
    [1] => stdClass Object (
        [lead_image_id] => 69
        [lead_image_name] => uploads/leads/5276.png
        [lead_image_lead_id] => 74
        [lead_image_status] => 0
    )
    [2] => stdClass Object (
        [lead_image_id] => 70
        [lead_image_name] => assets/images/logo.png
        [lead_image_lead_id] => 74
        [lead_image_status] => 0
    )
)

2 个答案:

答案 0 :(得分:2)

你可以像现在这样做。有2个数组firstarr是你的第一个数组,而secarr是第二个关联数组。

foreach($secarr as $secval){
    foreach($secaval as $val){
        if($val['lead_image_lead_id'] == $firstarr['value']){
            echo "match";
        }else{
            echo "not matched";
        }
    }
}

答案 1 :(得分:1)

你需要使用两个循环:

$source = [
          [
              'lead_image_id' => 60 ,
              'lead_image_name' => 'assets/images/logo.png ',
              'lead_image_lead_id' => 74 ,
              'lead_image_status' => 0
          ]
   ];

$searches = [72, 73, 74];

$result = [];

foreach($source as $value) {
    foreach($searches as $search) {
        if($value->lead_image_lead_id == $search) {
            $result[] = $value;
        }
    }
}    

var_dump($result);

或使用array_search

foreach($source as $value) {
    $r = array_search($value['lead_image_lead_id'], $searches);
    if($r !== false) {
        $result[] = $value;
    }
}