yoyoma
yoyoma

Reputation: 3536

Flatten php array inside another array at certain position

I have a function that returns an array like this

Array
(
[0] => Array
    (
        [name] => Percent
        [attributes] => Array
            (
                [Percent] => 50
            )
    )

[1] => Array
    (
        [name] => Percent
        [attributes] => Array
            (
                [Percent] => 50
            )
    )
)

I need to add each element above to another array like this

Array
(
    [0] => Array
      (
          [name] => Name
          [value] => Johan
      )
    [1] => Array
      (
          [name] => Address
          [value] => Mayfair Lane
      )
)

The thing is I need the first array elements inserted at the same level of the 2nd array, IN THE MIDDLE, between the 2nd arrays first and second elements.

Array
(
    [0] => Array
      (
          [name] => Name
          [value] => Johan
      )
    [1] => Array
    (
        [name] => Percent
        [attributes] => Array
            (
                [Percent] => 50
            )
    )
    [2] => Array
    (
        [name] => Percent
        [attributes] => Array
            (
                [Percent] => 50
            )
        )
    )
    [3] => Array
      (
          [name] => Address
          [value] => Mayfair Lane
      )
)

The problem is if I just add the first array as is, in the center (using its own variable), it adds it like this

Array
(
    [0] => Array
      (
          [name] => Name
          [value] => Johan
      )
    [1] => Array
      (
          [0] => Array
           (
               [name] => Percent
               [attributes] => Array
                (
                   [Percent] => 50
                )
           )
          [1] => Array
           (
               [name] => Percent
               [attributes] => Array
                (
                   [Percent] => 50
                )
           )
      )
    [2] => Array
      (
          [name] => Address
          [value] => Mayfair Lane
      )
)

I've looked at array_merge but ofcourse, I cannot specify the 2nd array elements go in the middle as I need.

Upvotes: 0

Views: 153

Answers (2)

rmondesilva
rmondesilva

Reputation: 1792

You don't need to do looping. Split them in 2. Merge the first part, the middle one you want to insert, the second part.

$arr1 = array(
    array(
        'name' => 'Percent',
        'attributes' => array('Percent'=>50)
    ),
    array(
        'name' => 'Percent',
        'attributes' => array('Percent'=>50)
    )
 );

$arr2 = array(
    array(
        'name'=>'Name',
        'value'=>'Johan'
        ),
    array(
        'name'=>'Address',
        'value'=>'Mayfair Lane'
        )
);

$mid = count($arr2)/2;

$chunks = array_chunk($arr2, $mid);

$merged = array_merge($chunks[0], $arr1, $chunks[1]);

var_dump($merged);

RESULT

array(4) {
  [0]=>
  array(2) {
    ["name"]=>
    string(4) "Name"
    ["value"]=>
    string(5) "Johan"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(7) "Percent"
    ["attributes"]=>
    array(1) {
      ["Percent"]=>
      int(50)
    }
  }
  [2]=>
  array(2) {
    ["name"]=>
    string(7) "Percent"
    ["attributes"]=>
    array(1) {
      ["Percent"]=>
      int(50)
    }
  }
  [3]=>
  array(2) {
    ["name"]=>
    string(7) "Address"
    ["value"]=>
    string(12) "Mayfair Lane"
  }
}

See for yourself

Upvotes: 1

user3927951
user3927951

Reputation:

You need array_splice(), e.g.:

array_splice($second, 1, 0, $first);

Upvotes: 2

Related Questions