1

I have an array like this.

array('root'=> array(
      'auth'=>'stringKey',
       'version'=>'4057',
       '...'=>'...'
  )
  )

Now I want to remove the outer array and root key and so I get array like below. i.e reduce it to one dimensional array

array(
  'auth'=>'stringKey',
   'version'=>'4057',
   '...'=>'...'
)
4
  • 4
    $myArray = $myArray['root']; Commented Jan 25, 2017 at 7:58
  • @MarkBaker copy to new array?. is there any better way to modify array in place as these are results from api calls and am worried about eating up memory Commented Jan 25, 2017 at 8:05
  • you can see my answer it works as you said Commented Jan 25, 2017 at 8:06
  • Better way! Why do you need it, because you can always directly reference the sub-array anyway using $myArray['root']... but I doubt if any other approach will be faster or more memory efficient Commented Jan 25, 2017 at 8:09

3 Answers 3

2
$array = array(
      'root'=> array(
           'auth'=>'stringKey',
           'version'=>'4057',
           '...'=>'...'
       )
);

print_r($array['root']);
Sign up to request clarification or add additional context in comments.

2 Comments

if the array is large, will the performance not degrade as there is coping involved
Maybe half of a millisecond slower. It shouldn't be an issue
2

Try this it is work for me

    <?php $myarr = array('root'=> array(
              'auth'=>'stringKey',
               'version'=>'4057',
               '...'=>'...'
          )
          );

        $array = call_user_func_array('array_merge',$myarr);
        print_r($array);

/*
output 

Array
(
    [auth] => stringKey
    [version] => 4057
    [...] => ...
)


*/
    ?>

3 Comments

let me do online example for you
you can see click here
its assuming there are multiple arrays as level one:
0

You can overwrite the existing variable with the :

$myarray = array('root'=> array(
      'auth'=>'stringKey',
       'version'=>'4057',
       '...'=>'...'
  )
  )

$myarray = $myarray['root'];

Check demo

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.