添加新行到数组

时间:2018-02-18 02:20:42

标签: php arrays array-merge

我有一个如下所示的数组:

Array (
 [class_name] => Array ( [0] => 1 [1] => 2 )
 [zone1_price] => Array ( [0] => 1 [1] => 2 )
 [zone2_price] => Array ( [0] => 1 [1] => 2 )
 [zone3_price] => Array ( [0] => 1 [1] => 2 )
 [zone4_price] => Array ( [0] => 1 [1] => 2 ) 
)

我希望能够添加新的行:

Array (
 [class_name] => Array ( [0] => 3 )
 [zone1_price] => Array ( [0] => 3 )
 [zone2_price] => Array ( [0] => 3 )
 [zone3_price] => Array ( [0] => 3 )
 [zone4_price] => Array ( [0] => 3 )
)

然后会变成:

Array (
 [class_name] => Array (  [0] => 1 [1] => 2 [2] => 3 )
 [zone1_price] => Array ( [0] => 1 [1] => 2 [2] => 3 )
 [zone2_price] => Array ( [0] => 1 [1] => 2 [2] => 3 )
 [zone3_price] => Array ( [0] => 1 [1] => 2 [2] => 3 )
 [zone4_price] => Array ( [0] => 1 [1] => 2 [2] => 3 )
)

我在想我需要使用array_merge,但我也认为我需要分离数组,给它们一些键,然后组合它们然后添加数组。

我从哪里开始这样的事情?

3 个答案:

答案 0 :(得分:2)

使用foreach遍历数组,我们将当前元素的键分配给$ key变量(在这种情况下,每个迭代键为class_name, zone1_price, zone2_price, zone3_price, zone4_price),我们使用array_push并添加新的原始数组的值。

此外,如果您想将新值分配到数组的开头,则可以使用array_unshift

$array = array(
  'class_name' => array(0 => 1, 1 => 2),
  'zone1_price' => array(0 => 1, 1 => 2),
  'zone2_price' => array(0 => 1, 1 => 2),
  'zone3_price' => array(0 => 1, 1 => 2),
  'zone4_price' => array(0 => 1, 1 => 2),
);

foreach($array as $key => $value){
  array_push($array[$key], 3);
}

结果:

Array
(
    [class_name] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [zone1_price] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [zone2_price] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [zone3_price] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [zone4_price] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

)

答案 1 :(得分:1)

另一种可能的方式

$array = array(
            'class_name' => array( '0' => 1, '1' => 2 ),
            'zone1_price' => array( '0' => 1, '1' => 2 ),
            'zone2_price' => array( '0' => 1, '1' => 2 ),
            'zone3_price' => array( '0' => 1, '1' => 2 ),
            'zone4_price' => array( '0' => 1, '1' => 2 )
    );

$array2 = array(
            'class_name' => array( '0' => 3 ),
            'zone1_price' => array( '0' => 3 ),
            'zone2_price' => array( '0' => 3 ),
            'zone3_price' => array( '0' => 3 ),
            'zone4_price' => array( '0' => 3 )
    );

print_r(array_merge_recursive($array, $array2));

答案 2 :(得分:0)

你可以这样做

<?php
  $first=array(
    'class_name'=>array(1,2),
    'zone1_price'=>array(1,2),  
    'zone2_price'=>array(1,2),  
    'zone3_price'=>array(1,2),  
    'zone4_price'=>array(1,2),
    );
  $second=array(
    'class_name'=>array(3,4),
    'zone1_price'=>array(3,4),  
    'zone2_price'=>array(3,4),  
    'zone3_price'=>array(3,4),  
    'zone4_price'=>array(3,4),  
    );
  $fs=sizeof($first['class_name']);
  $ss=sizeof($second['class_name']);
  foreach($first as $k => $v)
  {
    for($i=$fs;$i<$ss+$fs;$i++)
    {
      $first[$k][$i]=$second[$k][$i-$fs];
    } 
  }