合并对象数组,一旦合并删除以前的索引?

时间:2017-10-03 07:52:39

标签: php arrays object multidimensional-array

我有一个对象数组,我需要组合具有相同键的子数组。这是我的原始阵列。

0 => 
    object(Template)[59]
      protected 'values' => 
        array (size=3)
          'part' => string 'hinge'
          'part_status' => string 'warehouse'
          'part_type' => string 'consumable'
1 => 
array (size=1)
  '3741852' => 
    object(Template)[61]
      protected 'values' => 
        array (size=4)
          'partNum' => string '3741852'
          'part' => string 'bolt'
          'part_status' => string 'shipped'
          'part_type' => string 'consumable'
2 => 
array (size=1)
  '3741852' => 
    object(Template)[61]
      protected 'values' => 
        array (size=4)
          'partNum' => string '3741852'
          'part' => string 'bolt'
          'part_status' => string 'shipped'
          'part_type' => string 'consumable'

最终我需要做的是将数组与索引3741852结合起来并将它们放在同一个父索引下。

所以我设法完成了这个,就像这样。

foreach ($array as $key => $value) {
  foreach ($value as $data) {
    $partDetails[][$data['partNum']] = $details;
    if ($key === $data['partNum']) {
         $partDetails[$key] = $details;
    }
  }
}

但我不确定如何删除以前的索引[2]?这就是我现在所拥有的。

0 => 
    object(Template)[59]
      protected 'values' => 
        array (size=3)
          'part' => string 'hinge'
          'part_status' => string 'warehouse'
          'customer_type' => string 'consumable'
1 => 
  array (size=1)
  '3741852' => 
    object(Template)[61]
      protected 'values' => 
        array (size=4)
          'partNum' => string '3741852'
          'part' => string 'bolt'
          'part_status' => string 'shipped'
          'part_type' => string 'consumable'
  '3741852' => 
    object(Template)[61]
      protected 'values' => 
        array (size=4)
          'partNum' => string '3741852'
          'part' => string 'bolt'
          'part_status' => string 'shipped'
          'part_type' => string 'consumable'
2 => 
array (size=1)
  '3741852' => 
    object(Template)[61]
      protected 'values' => 
        array (size=4)
          'partNum' => string '3741852'
          'part' => string 'bolt'
          'part_status' => string 'shipped'
          'part_type' => string 'consumable'

1 个答案:

答案 0 :(得分:0)

使用:

unset( $array[ $index ] ); // remove index from array
$array = array_values($array); // It will reindex array again