Jon Doe
Jon Doe

Reputation: 751

How to indent arrays in PHP

How can I indent arrays in PHP?

Say we have the following array

[Array] (
   [red] => 10
   [green] => 20
   [blue] => 30
)

How can I indent it in one level, so that I have this at the end

[Array] (
    Colors => [Array] (
       [red] => 10
       [green] => 20
       [blue] => 30
   )
)

Upvotes: 1

Views: 1644

Answers (3)

Gaurav
Gaurav

Reputation: 28755

$array['Colors'] = $origArray;

Upvotes: 0

deceze
deceze

Reputation: 522175

$array = array('Colors' => $array);

Upvotes: 1

Pascal MARTIN
Pascal MARTIN

Reputation: 401032

Basically, you want your first array to become embeded in another one ?

If so, what about :

$newArray = array(
    'Colors' => $yourOldArray
);

Of course, $yourOldArray is the variable that contains your old array ;-)
Or you can replace the $yourOldArray part by the content of that old array itself.


Of, if the new array already exists, and you just want to add a sub-item to it :

$newArray['Colors'] = $yourOldArray;

Upvotes: 3

Related Questions