1

I have two array of values and their keys...
First array

    Array
    (
    [0] => Array
        (
            [10] => A1
            [11] => A2
        )

    [1] => Array
        (
            [12] => B1
            [13] => B2
        )

)

Second array

Array
(
  [1] => Z1
  [2] => Z2
)

I want to group these two array into a single array. I mean the array format should be:

Array
(
[0] => Array
    (
        [1] => Z1
        [10] => A1
        [11] => A2
    )

[1] => Array
    (
        [2] => Z2
        [12] => B1
        [13] => B2
    )
 )

I tried with array_push but add the entire array in the [0] position or in the [2] position in the second array.

Anyone has any ideas?

2
  • 1
    In what programming language? Commented Mar 7, 2012 at 9:24
  • PHP. Oops, I forgot to mention that. Commented Mar 7, 2012 at 9:27

1 Answer 1

1

you can try this code

$arrOne = array(
    0 => array(
        10 => 'A1',
        11 => 'A2'
    ),
    1 => array(
        12 => 'B1',
        13 => 'B2'
    )
);

$arrTwo = array(
    1 => 'Z1',
    2 => 'Z2'
);
$arrcountone = count($arrOne);
$arrcounttwo = count($arrTwo);
$i=0;
foreach ($arrOne as $key1 => $value1) {
    $i++;$k=0;
    foreach ($arrTwo as $key => $value) {
        $k++;
        if($i == $k){
            $arrOne[$key1][$key] = $value;
        }
    }
}

var_dump($arrOne) gives

 array
      0 => 
        array
          1 => string 'Z1' (length=2)
          10 => string 'A1' (length=2)
          11 => string 'A2' (length=2)
      1 => 
        array
          2 => string 'Z2' (length=2)
          12 => string 'B1' (length=2)
          13 => string 'B2' (length=2)
Sign up to request clarification or add additional context in comments.

1 Comment

+1 I realized that my answer didn't preserve the array keys. You beat me to it.

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.