php在动态数组中合并/合并元素

时间:2016-11-16 02:02:08

标签: php arrays array-merge

我有一个来自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)

根据数组:

$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索引?

1 个答案:

答案 0 :(得分:1)

  

您可以简单地将每个子数组作为参数传递给array_merge(),而无需循环。但是,最好记住,对于array_merge(),后面的数组的值将覆盖上一个数组的值,因此最重要的是您最感兴趣的数据的数组。下面的代码说明了这一点。一个Quick-Test Here

GIVEN ARRAY:

<?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);

DUMP结果:

    // 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)
  

更新:使用循环(手动合并)。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)