php combine/merge 动态数组中的元素

php combine/merge elements in dynamic array

我有一个 post 形式的数组,输入变成了一个数组,它至少有一个元素。它看起来像这样:

'saleTimes' => 
 array 
  0 => 
    array (size=9)
      'sunSale' => int 0
      'monSale' => string '1' (length=1)
      'tueSale' => int 0
      'wedSale' => int 0
      'thuSale' => int 0
      'friSale' => int 0
      'satSale' => int 0
      'startSaleTime' => string '09:00' (length=5)
      'endSaleTime' => string '15:00' (length=5)
  1 => 
    array (size=9)
      'sunSale' => int 0
      'monSale' => int 0
      'tueSale' => string '1' (length=1)
      'wedSale' => int 0
      'thuSale' => int 0
      'friSale' => int 0
      'satSale' => int 0
      'startSaleTime' => string '05:19' (length=5)
      'endSaleTime' => string '08:00' (length=5)
  2 => 
    array (size=9)
      'sunSale' => int 0
      'monSale' => int 0
      'tueSale' => string '1' (length=1)
      'wedSale' => int 0
      'thuSale' => int 0
      'friSale' => int 0
      'satSale' => string '1' (length=1)
      'startSaleTime' => string '05:19' (length=5)
      'endSaleTime' => string '08:00' (length=5)
   .....

我想将元素合并为一个,结果是这样的:

array (size=9)
  'sunSale' => int 0
  'monSale' => string '1' (length=1)
  'tueSale' => string '1' (length=1)
  'wedSale' => int 0
  'thuSale' => int 0
  'friSale' => int 0
  'satSale' => string '1' (length=1)

AND 根据数组:

$weekName = array(
    'sunSale' => '星期日',
    'monSale' => '星期一',
    'tueSale' => '星期二',
    'wedSale' => '星期三',
    'thuSale' => '星期四',
    'friSale' => '星期五',
    'satSale' => '星期六',
);

会保留weeks索引,值为string '1'startSaleTimeendSaleTime没有用,保留在数组中或删除它们 不影响结果。

我试过了

for($i = 0; $i < count($weekName); $i++) {
       array_merge($paramList['saleTimes'][$i], (int)$paramList['saleTimes'][$i+1]);
    }

但会报错Argument #1 is not an array, 如何获得值为 1 的所有 weeks 索引?

You can simply pass each single sub-array to array_merge() as Arguments without the need for a Loop. However, it is good to remember that for an array_merge(), the values of the later Array will override those of Previous one, so it is important that the array whose Data you are most interested in comes last. The Code below demonstrates this Point. A Quick-Test Here:

给定数组:

<?php
    $arr        = array(
        array(
          'sunSale' =>  0,
          'monSale' => '1',
          'tueSale' =>  0,
          'wedSale' =>  0,
          'thuSale' =>  0,
          'friSale' =>  0,
          'satSale' =>  0,
          'startSaleTime' => '09:00',
          'endSaleTime' => '15:00',
        ),
        array (
          'sunSale' =>  0,
          'monSale' =>  0,
          'tueSale' => '1',
          'wedSale' =>  0,
          'thuSale' =>  0,
          'friSale' =>  0,
          'satSale' =>  0,
          'startSaleTime' => '05:19',
          'endSaleTime' => '08:00',
        ),
        array (
          'sunSale' =>  0,
          'monSale' =>  0,
          'tueSale' => '1',
          'wedSale' =>  0,
          'thuSale' =>  0,
          'friSale' =>  0,
          'satSale' => '1',
          'startSaleTime' => '05:19',
          'endSaleTime' => '08:00',
        ),
    );

合并:

<?php
    $arrMerged  = array_merge($arr[0], $arr[1], $arr[2]);

    var_dump($arrMerged);

转储结果:

    // var_dump($arrMerged) ABOVE YIELDS::      
    array (size=9)
      'sunSale' => int 0
      'monSale' => int 0
      'tueSale' => string '1' (length=1)
      'wedSale' => int 0
      'thuSale' => int 0
      'friSale' => int 0
      'satSale' => string '1' (length=1)
      'startSaleTime' => string '05:19' (length=5)
      'endSaleTime' => string '08:00' (length=5)

UPDATE: USING A LOOP (MANUAL MERGING). (QUICK-TEST HERE.)

<?php

    $merged     = $arr[0];

    foreach($arr as $item){
        foreach($item as $key=>$val){
            $merged[$key] = ($merged[$key] == "" || !$merged[$key])?$val : $merged[$key];
        }
    }
    // REMOVE THE `startSaleTime` AND `endSaleTime` KEYS
    unset($merged['startSaleTime']);
    unset($merged['endSaleTime']);

    var_dump($merged);
    // YIELDS::
    array (size=7)
      'sunSale' => int 0
      'monSale' => string '1' (length=1)
      'tueSale' => string '1' (length=1)
      'wedSale' => int 0
      'thuSale' => int 0
      'friSale' => int 0
      'satSale' => string '1' (length=1)