0

I get this array, but I want to show me KEY and all subitem of that KEY bellow, I dont want every time to repear a key, I need that key shows only one and all others entity that has same key under this one.

 array(2) {
      [0]=>
      array(1) {
        ["A"]=>
        string(2) "Test1"
      }
      [1]=>
      array(1) {
        ["A"]=>
        string(2) "Test1"
      }
    }

I want something like this:

 array(2) {
      [0]=>
      array(1) {
        ["A"]=>
       string(2) "Test1", 
        string(2) "Test2"
      }
     }

1 Answer 1

2

use the following:

$return = array_merge_recursive($array, ...);

in your case:

$return = array_merge_recursive($array[0], $array[1]);

but I am guessing you'll want a more dynamic solution, I will draft that up now.

$return = call_user_func_array('array_merge_recursive', $array);
Sign up to request clarification or add additional context in comments.

3 Comments

But it is in same array, i have tried to merge it with empty array but doesnt work, and also I have tried to merge it with it self
look at the 'in your case part'
@user3042036 please check out the latter 2 snippets as they can both be used in your situation, the final one being dynamically adapted to any amount of array elements.

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.