0

I have an array which looks like the following:

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

I want to be able to add new rows which will be:

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

which would then become:

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

I am thinking I will need to use array_merge but I also think I need to separate the arrays, give them a key of some sort then combine them and add then array them.

Where would I start with something like this?

0

3 Answers 3

2

You loop through the array with foreach, we assign the current element's key to the $key variable (in this case, each iteration key are class_name, zone1_price, zone2_price, zone3_price, zone4_price), we use array_push and to add the new values to the original array.

Additionally, if you'd like to assign the new values to the beginning of the array, you can use 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);
}

Results:

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
        )

)
Sign up to request clarification or add additional context in comments.

Comments

1

Another possible way

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

1 Comment

@Gareth This is what I would do. Your task is exactly what this function was designed to do. Do you have any fringe cases where this is not suitable?
0

You can do it like this

<?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];
    } 
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.