【问题标题】:How to compare 2 array of PHP objects then fill in empty string if data not match?如果数据不匹配,如何比较 2 个 PHP 对象数组然后填充空字符串?
【发布时间】:2019-08-17 12:40:04
【问题描述】:

有2个不同大小的数组,如果数据不匹配,数组需要用空字符串填充。

数组 A,里面有 3 个对象:

$array_A = [
             {"code":"1","cost":30},
             {"code":"3","cost":100},
             {"code":"4","cost":50}
          ]

数组 B 里面有 5 个对象:

$array_B = [
             {"code":"1"},
             {"code":"2"},
             {"code":"3"},
             {"code":"4"},
             {"code":"5"}
          ]

然后,我需要将数组A和数组B与属性code进行比较,如果code相同,则填写cost属性,否则让cost == 0。 如下图:-

新数组:

$array_New = [
             {"code":"1","cost":30},
             {"code":"2","cost":0},
             {"code":"3","cost":100},
             {"code":"4","cost":50},
             {"code":"5","cost":0}
          ]

这是我的解决方案(但不起作用):

用两个for循环比较数组A和数组B,然后将数据填充到array_New

$array_New= array();

foreach ($array_B as $key => $array_B_data) {
    for ($i=0; $i < sizeof($array_A) ; $i++) { 
    if ($array_B_data->code == $array_A[$i]->code) {    

        $array_New[$key] = $array_A[$i]['cost'];
    }else{
        $array_New[$key] = '';

        }
    }

}

return $array_New;

但是,$array_New 不显示 Array New 中所示的数据。 谁能给我一些指导? 非常感谢。

【问题讨论】:

  • 为什么json值在数组中?为什么不始终具有对称结构?
  • 其实我的数据是从 Laravel PHP 模型生成的。上面的代码我只是让它更容易理解。这是包含 PHP 对象的数组。

标签: php arrays object compare


【解决方案1】:

试试这个!

new_array= array();
foreach ($array_B as $key => $data) {
    $found = false;
    $cost = 0;
    for($i=0;$i<count($array_A);$i++){
        if($array_A[$i]['code']==$data['code']){
        $found = true;
        $cost = $array_A[$i]['cost'];
    }
}
// print_r($data);
if (!$found){
  $new_array[count($new_array)+1] = ['code'=>$data['code'], 'cost'=>$cost];
}else{
  $new_array[count($new_array)+1] = ['code'=>$data['code'], 'cost'=>$cost];
}
}

【讨论】:

  • @Woshington Valdeci,感谢您的帮助。这对我来说是正确的答案。谢谢!
【解决方案2】:

使用关联数组将比 2 个嵌套循环更快。下面是sn-p的代码:

<?php

function make($code,$cost = null){
    $o = new stdclass();
    $o->code = $code;
    if(!is_null($cost)) $o->cost = $cost;
    return $o;
}

$array_A = [
            make("1",30),
            make("3",100),
            make("4",50),
          ];

$array_B = [
            make("1"),
            make("2"),
            make("3"),
            make("4"),
            make("5")
          ];

$set = [];


foreach($array_A as $current){
    if(!property_exists($current,'cost')) $current->cost = 0;
    if(!isset($set[$current->code]) || $set[$current->code]->cost == 0 && $current->cost > 0) $set[$current->code] = $current;
}

foreach($array_B as $current){
    if(!property_exists($current,'cost')) $current->cost = 0;
    if(!isset($set[$current->code]) || $set[$current->code]->cost == 0 && $current->cost > 0) $set[$current->code] = $current;
}


print_r(array_values($set));

演示: https://3v4l.org/9IWcp

  • 我们创建一个数组$set,其中键是code,值是对象本身,如{code:xyz,cost:xyz}

  • 我们在两个数组的元素之间移动,如果一个元素没有cost 属性,我们将cost 显式分配为0 并将其插入到我们的$set 中。

  • 如果 $set 已经将该代码作为键,我们将比较 $current 的成本和存储的成本,并相应地插入非零成本值对象(如果存在)。

【讨论】:

  • 感谢您的耐心解释!欣赏!
猜你喜欢
  • 2013-12-19
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-12
  • 1970-01-01
相关资源
最近更新 更多