0

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.

8
  • i could do it with a foreach loop Commented Mar 28, 2016 at 2:05
  • yeh @Dragon foreach would work but I wasnt sure if its the most efficient. Would u use array_splice in there ? Commented Mar 28, 2016 at 2:05
  • Yes, you can post it also. So we can see the problem in your foreach algorithm. Commented Mar 28, 2016 at 2:06
  • @yoyoma May I ask you, why do you need it at all? The question looks really unjustified and strange, except it is an interview or a training question. Commented Mar 28, 2016 at 2:19
  • are u kidding @Axalix ? Its a legitimate business problem i have Commented Mar 28, 2016 at 2:21

2 Answers 2

2

You need array_splice(), e.g.:

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

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

5 Comments

thought about that - but its gets messy depending on how many inner elements u need to add - that position param needs counter and again, not sure if its the cleanest solution ?
I'm not sure what could be cleaner than a single function call. Just to clarify - my example will put all of the elements from $first into $second at position 1, i.e. after the first element which is already in $second. The elements will be put at the same "level". More information on array_splice() is available in the Manual. Depending on your definition of "middle", you could update the second parameter accordingly, e.g. array_splice($second, count($second) / 2, 0, $first);
yes i know wot u are saying, wot i mean by not clean is the fact that what about if the inner array has 5 elements, 10 elements, 50 elements ? I will need a counter in the foreach loop to keep track for that position param. Not saying its incorrect, just looking for nicest solution
Your question was this: "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." That's what the code in my answer does. The number of items in the inner array (i.e. $first) doesn't matter and no loop is required to meet this requirement. Possibly you wanted something different from what you put in your question but I'm afraid I can't read your mind :)
@yoyoma the suggested decision doesn't depend on the number of elements in the array you are embedding. Just give it a try. "1" means "skip first element" and "0" - "don't touch the original array".
1

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

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.