laravel php将数组与对象数组进行比较/差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16689792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:48:15  来源:igfitidea点击:

php compare/diff an array with an array of objects

phparrayscomparedifflaravel

提问by feub

I have 2 PHP arrays, a simple one:

我有 2 个 PHP 数组,一个简单的数组:

array
  0 => int 5
  1 => int 6

and an array of objects:

和一组对象:

array
  0 => 
    object(stdClass)[43]
      public 'id' => int 1
  1 => 
    object(stdClass)[46]
      public 'id' => int 3
  2 => 
    object(stdClass)[43]
      public 'id' => int 5
  3 => 
    object(stdClass)[46]
      public 'id' => int 6
  4 => 
    object(stdClass)[46]
      public 'id' => int 7

I'd like to make a diff of these 2 arrays to eliminate in the second those present in the first. In this example, i don't want the ids 5 and 6 in the second array. But i need help ;>

我想对这两个数组进行比较,以在第二个数组中消除第一个数组中存在的数组。在这个例子中,我不想要第二个数组中的 ids 5 和 6。但我需要帮助;>

Thank you.

谢谢你。

fabien

法比安

采纳答案by Glitch Desire

Assuming $objectsis your array of objects, and $valuesis your array of values to remove...

假设$objects是您的对象数组,并且$values是要删除的值数组...

You could use a foreachloop if you want to return objects:

foreach如果要返回对象,可以使用循环:

$values = array(5, 6);
$objects = array(
    (object) array("id" => 1),
    (object) array("id" => 3),
    (object) array("id" => 5),
    (object) array("id" => 6),
    (object) array("id" => 7)
);
foreach($objects as $key => $object) {
    if(in_array($object->id,$values)) {
        unset($objects[$key]);
    }
}

Live demo(0.008 sec)

现场演示(0.008 秒)

If you want to use the difffunction itself (that's possible but awkward, less readable and will just return an array of values) you can (as Baba suggested) return the idof the object inline:

如果你想使用diff函数本身(这可能但很笨拙,可读性较差,只会返回一个值数组),你可以(如 Baba 建议的那样)id内联返回对象的 :

$values = array(5, 6);
$objects = array(
    (object) array("id" => 1),
    (object) array("id" => 3),
    (object) array("id" => 5),
    (object) array("id" => 6),
    (object) array("id" => 7)
);
$diff = array_diff(array_map(function ($object) {
    return $object->id;
}, $objects), $values);

Live demo(0.008 sec)

现场演示(0.008 秒)

回答by Baba

You can try :

你可以试试 :

$diff = array_diff(array_map(function ($v) {
    return $v->id;
}, $array2), $array1);

See Live DEMO

观看现场演示

回答by Nick Maroulis

For versions older than 5.3

对于 5.3 之前的版本

 foreach( $arr_2nd as $key => $val  )
{
   $arr_2nd[$key] = $val->id;
}

array_diff( $arr_1st, $arr_2nd );

回答by fullybaked

Loop over the second array and use in_arraymethod to check for existing values in first

循环遍历第二个数组并使用in_array方法检查第一个中的现有值

$firstArray = array(5, 6);
foreach ($objects as $key => $object) {
    if (in_array($object->id, $firstArray)) {
        unset($objects[$key];
    }
}