如果数据不匹配,如何比较2个PHP对象数组,然后填充空字符串?
有2个大小不同的数组,如果数据不匹配,则需要用空字符串填充数组 包含3个对象的数组:如果数据不匹配,如何比较2个PHP对象数组,然后填充空字符串?,php,arrays,object,compare,Php,Arrays,Object,Compare,有2个大小不同的数组,如果数据不匹配,则需要用空字符串填充数组 包含3个对象的数组: $array_A = [ {"code":"1","cost":30}, {"code":"3","cost":100}, {"code":"4","cost":50} ] $array_B = [ {"code":"1"}, {"code":"2"},
$array_A = [
{"code":"1","cost":30},
{"code":"3","cost":100},
{"code":"4","cost":50}
]
$array_B = [
{"code":"1"},
{"code":"2"},
{"code":"3"},
{"code":"4"},
{"code":"5"}
]
包含5个对象的数组B:
$array_A = [
{"code":"1","cost":30},
{"code":"3","cost":100},
{"code":"4","cost":50}
]
$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}
]
这是我的解决方案(但无法奏效):
将数组A和数组B与两个for循环进行比较,然后将数据填充到数组_New中
$array_New=array();
foreach($array\u B as$key=>$array\u B\u data){
对于($i=0;$icode==$array_A[$i]->code){
$array_New[$key]=$array_A[$i]['cost'];
}否则{
$array_New[$key]='';
}
}
}
返回$array\u New;
但是,$array_New不会显示array New中显示的数据。
有人能给我一些指导吗?
非常感谢。试试这个
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];
}
}
new_array=array();
foreach($key=>$data的数组){
$found=false;
$cost=0;
对于($i=0;$i$data['code'],'cost'=>$cost];
}否则{
$new_数组[计数($new_数组)+1]=['code'=>$data['code'],'cost'=>$cost];
}
}
试试这个
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];
}
}
new_array=array();
foreach($key=>$data的数组){
$found=false;
$cost=0;
对于($i=0;$i$data['code'],'cost'=>$cost];
}否则{
$new_数组[计数($new_数组)+1]=['code'=>$data['code'],'cost'=>$cost];
}
}
使用关联数组将比使用两个嵌套循环快得多。下面是代码片段:
<?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));
使用关联数组将比使用两个嵌套循环快得多。下面是代码片段:
<?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));
为什么数组中包含json值?为什么不包含对称结构?实际上,我是从Laravel PHP模型生成的数据。上面的代码只是让它更容易理解。这是包含PHP对象的数组。为什么数组中包含json值?为什么不包含对称结构?实际上,我是从我是Laravel PHP模型。上面的代码我只是让它更容易理解。这是带有PHP对象的数组。@Woshington Valdeci,谢谢你的帮助。这是我的正确答案。谢谢!@Woshington Valdeci,谢谢你的帮助。这是我的正确答案。谢谢!谢谢你耐心的解释!谢谢!谢谢你耐心的解释!谢谢!