如何在PHP中合并多维数组中的数组

时间:2018-08-17 11:12:50

标签: php laravel

如何在多维数组中合并数组。我很困惑

我有2个数组

$getLate = [
  0 => {
    "id": 1
    "name": "student1"
    "totalLate": 1
  }
  1 => {
    "id": 3
    "name": "student2"
    "totalLate": 1
  }
]


$getName = [ 
0 => {
    +"id": 1
    +"name": "student1"
  }
  1 => {
    +"id": 3
    +"name": "student2"
  }
  2 => {
    +"id": 4
    +"name": "student3"
  }

我想检查一下:

if $getName['name'] = $getLate['name']我想将totalLate添加到$getName;

如何合并它们,以使输出看起来像这样

$getLate = [
  0 => {
    "id": 1
    "name": "student1"
    "totalLate": 1
  }
  1 => {
    "id": 3
    "name": "student2"
    "totalLate": 1
  }
  2 => {
    +"id": 4
    +"name": "student3"
    "totalLate": 0
  }
]

1 个答案:

答案 0 :(得分:0)

$output = array();
foreach ($getLate as $key => $value){
    $output[] = (object)array_merge((array)$getName[$key], (array)$value);
}
print_r($output);