Search code examples
phparraysarray-merge

php combine/merge elements in dynamic array


I have an array from post form, the inputs turned to an array and It has one element at least. It look like this:

'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)
   .....

I want to merge elements to one, the result will be this:

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 according to an array:

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

It will remain weeks index which has value string '1', and startSaleTimeendSaleTime is no use, keep them in array or remove them does not affect the result.

I tried this

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

but will get error Argument #1 is not an array, how can I get the all weeks index with value 1 ?


Solution

  • 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:

    THE 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',
            ),
        );
    

    THE MERGE:

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

    THE DUMP RESULT:

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