PHP 2数组相同的键合并值

时间:2016-03-02 22:12:39

标签: php arrays array-merge

我有2个具有相同键但值不同的数组。我试图将值合并到一个新数组。我试过了array_merge,array_merge_recursive,array1 + array2,array_intersect,array_intersect_key(array_merge())

MAIN ARRAY

array (size=2)
'attr' => 
array (size=6)
  'attr__6__23__grad_gown_size' => 
    array (size=3)
      'val' => string '5ft.9in. - 5ft.11in.' (length=20)
      'qty' => string '1' (length=1)
      'ordqty' => 
        array (size=1)
          ...
  'attr__1__23__grad_dvd' => 
    array (size=3)
      'val' => null
      'qty' => string '0' (length=1)
      'ordqty' => 
        array (size=1)
          ...
  'attr__2__23__grad_combo' => 
    array (size=3)
      'val' => string 'Yes' (length=3)
      'qty' => string '1' (length=1)
      'ordqty' => 
        array (size=1)
          ...
  'attr__3__23__grad_tas_dip_only' => 
    array (size=3)
      'val' => null
      'qty' => string '0' (length=1)
      'ordqty' => 
        array (size=1)
          ...
  'attr__4__23__grad_tassel' => 
    array (size=3)
      'val' => null
      'qty' => string '0' (length=1)
      'ordqty' => 
        array (size=1)
          ...
  'attr__5__23__grad_honor_seal' => 
    array (size=3)
      'val' => null
      'qty' => string '0' (length=1)
      'ordqty' => 
        array (size=1)
          ...
'attr_add' => 
array (size=6)
  'attr__6__23__grad_gown_size' => 
    array (size=3)
      'val' => string '' (length=0)
      'qty' => string '0' (length=1)
      'ordqty' => 
        array (size=0)
          ...
  'attr__1__23__grad_dvd' => 
    array (size=3)
      'val' => string 'Yes' (length=3)
      'qty' => string '1' (length=1)
      'ordqty' => 
        array (size=0)
          ...
  'attr__2__23__grad_combo' => 
    array (size=3)
      'val' => null
      'qty' => string '0' (length=1)
      'ordqty' => 
        array (size=0)
          ...
  'attr__3__23__grad_tas_dip_only' => 
    array (size=3)
      'val' => null
      'qty' => string '0' (length=1)
      'ordqty' => 
        array (size=0)
          ...
  'attr__4__23__grad_tassel' => 
    array (size=3)
      'val' => string 'Yes' (length=3)
      'qty' => string '1' (length=1)
      'ordqty' => 
        array (size=0)
          ...
  'attr__5__23__grad_honor_seal' => 
    array (size=3)
      'val' => string 'Yes' (length=3)
      'qty' => string '1' (length=1)
      'ordqty' => 
        array (size=0)
          ...

Array 1 attr

array (size=3)
'val' => string '5ft.9in. - 5ft.11in.' (length=20)
'qty' => string '1' (length=1)
'ordqty' => 
array (size=1)
  27 => string '1' (length=1)

array (size=3)
'val' => null
'qty' => string '0' (length=1)
'ordqty' => 
array (size=1)
  27 => string '0' (length=1)

数组2 attr_add

array (size=3)
'val' => string '' (length=0)
'qty' => string '0' (length=1)
'ordqty' => 
array (size=1)
  30 => string '0' (length=1)

array (size=3)
'val' => string 'Yes' (length=3)
'qty' => string '1' (length=1)
'ordqty' => 
array (size=1)
  30 => string '1' (length=1)

结果

 array (size=1)
'attr' => 
  array (size=6)
  'attr__6__23__grad_gown_size' => 
    array (size=3)
      'val' => string '5ft.9in. - 5ft.11in.' (length=20)
      'qty' => string '1' (length=1)
      'ordqty' => 
        array (size=2)
          27 => string '1' (length=1)
          30 => string '0' (length=1)
  'attr__1__23__grad_dvd' => 
    array (size=3)
      'val' => Yes
      'qty' => string '1' (length=1)
      'ordqty' => 
        array (size=1)
          27 => string '0' (length=1)
          30 => string '1' (length=1)

我正在尝试

foreach($extra_fields_array['attr'] as $key=>$value)
{
    $ovalue = $extra_fields_array['attr'][$key]; // Array 1
    $nvalue = $attr_array['attr_add'][$key]; // Array 2

    $new_array['attr'][$key] = array_merge($nvalue,$ovalue); // NOT WORKING
 /*
  * What do I do here
  */

}
echo '<pre>';   var_dump($new_array);echo '</pre>';

我希望这是有道理的。我整天都被困在上面。 感谢您提前提供任何帮助。

0 个答案:

没有答案