-2

I have two arrays. One is a multidimensional array and the other is structured normally as seen below.

 Array ( 
           [0] = Array
              (
                 [0] => Array 
                        (
                         [james] => 1
                         [kevin] => 2
                        )
                 [1] => Array
                        (
                         [joe] => 1
                         [jim] => 2
                        )
               )
           [1] = Array
              (
                 [0] => Array 
                        (
                         [jill] => 1
                         [john] => 2
                        )
                 [1] => Array
                        (
                         [janet] => 1
                         [clarence] => 2
                        )
               )
      )

and the second array

Array
(
    [0] => Array
        (
            [total_stuff] => 75210

        )

    [1] => Array
        (
            [total_stuff] => 95640

        )

)

How would I append the first value of the second array to the end of the first inner array within the multidimensional array so it would look like the array that follows? I need to preserve the values of the second array but not the keys.

 Array ( 
               [0] = Array
                  (
                     [0] => Array 
                            (
                             [james] => 1
                             [kevin] => 2
                            )
                     [1] => Array
                            (
                             [joe] => 1
                             [jim] => 2
                            )
                     [2] => Array
                            (
                            [total_stuff] => 75210
                            )
                   )
               [1] = Array
                  (
                     [0] => Array 
                            (
                             [jill] => 1
                             [john] => 2
                            )
                     [1] => Array
                            (
                             [janet] => 1
                             [clarence] => 2
                            )
                     [2] => Array
                            (
                            [total_stuff] => 95640
                            )
                   )
          )
sumit
  • 15,003
  • 12
  • 69
  • 110
WA Martin
  • 7
  • 2
  • 1
    can you please show us your attempt? – sumit May 08 '18 at 23:02
  • It's hard to give you a solution without knowing your code, var names, or the context you need this to work in. E.g. is it always "total_Stuff", will it always be the first value? etc – James May 08 '18 at 23:20
  • [Implementation of technique from dupe target](https://3v4l.org/EmDML) – mickmackusa Sep 24 '22 at 11:50

1 Answers1

0

You can do it using array map and array merge

<?php
$a1=array ( 
           array
              (
                  array 
                        (
                         "james" => 1,
                         "kevin" => 2
                        ),
                 array
                        (
                         "joe" => 1,
                         "jim" => 2
                        )
               ),
           array
              (
                  array 
                        (
                         "jill" => 1,
                         "john" => 2
                        ),
                   array
                        (
                         "janet" => 1,
                         "clarence" => 2
                        )
               )
      );

$a2=array
(
     array
        (
            "total_stuff" => 75210

        ),

    array
        (
            "total_stuff" => 95640

        )

);
//merge each index with corresponding index of second array to form new array as you desired
$new = array_map(function ($a,$k)use($a2) { return array_merge($a,array($a2[$k])); }, $a1,array_keys($a1));
echo "<pre>";
print_r($new);
?>

output

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [james] => 1
                    [kevin] => 2
                )

            [1] => Array
                (
                    [joe] => 1
                    [jim] => 2
                )

            [2] => Array
                (
                    [total_stuff] => 75210
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [jill] => 1
                    [john] => 2
                )

            [1] => Array
                (
                    [janet] => 1
                    [clarence] => 2
                )

            [2] => Array
                (
                    [total_stuff] => 95640
                )

        )

)

working fiddle http://phpfiddle.org/main/code/ymf7-69si

sumit
  • 15,003
  • 12
  • 69
  • 110