1

plz anyone help me. I want to merge the subarrays. I have associative array to merge in php like below.but the arrays were flatter. I tried to adapt the code, but unfortunately without success. Here my example :

Array(
 [0] => Array(
        [1] => Array(
                [pid] => 1278
                [price] => 30
            )
        [2] => Array (
                [pid] => 1279
                [price] => 300
            )
    )
    [1] => Array (
        [1] => Array (
                [pid] => 1280
                [price] => 120
            ) 
        [2] => Array (
                [pid] => 1281
                [price] => 250
            )
)

And i have to Generate like below.

Array (
    [1] => Array (
            [pid] => 1278
            [price] => 30
        )
    [2] => Array (
            [pid] => 1279
            [price] => 300
        )
    [3] => Array (
            [pid] => 1280
            [price] => 120
        )
    [4] => Array (
            [pid] => 1281
            [price] => 250
        )
)
1
  • point a new variable to the top level array first index, than newvars contains what you want $newvar = $array[0]; Commented Aug 31, 2016 at 13:02

1 Answer 1

3

Try this

$a = array(
   array(
      1 => array(
            'pid' =>  1234
            'price' => 200
         )
      2 => array(
            'pid' =>  1234
            'price' => 200
         ) 
    ),
    array(
      1 => array(
            'pid' =>  1234
            'price' => 200
         )
      2 => array(
            'pid' =>  1234
            'price' => 200
         ) 

    );
$a = call_user_func_array('array_merge',$a);
print_r($a);
Sign up to request clarification or add additional context in comments.

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.