user3199862
user3199862

Reputation: 1

change array name in array with array_push function

I have this code:

 $array = array ('item' =>array("title" => "Revolution","size" => "100", "link" => "www"));
 $add = array("title" => "Revolution","size" => "100", "link" => "www");
 array_push($array, $add);
 print_r ($array);

and result is this:

Array
(
[item] => Array
    (
        [title] => Revolution
        [size] => 100
        [link] => www
    )

[0] => Array
    (
        [title] => Revolution
        [size] => 100
        [link] => www
    )

)

How to change the [0] to [item:1], i'm lost.

Thanks Michael

Upvotes: 0

Views: 138

Answers (3)

tyteen4a03
tyteen4a03

Reputation: 1932

I think you mean $array["items"][1]. Push to $array["items"] instead.

Alternatively, just do $array["items"][] = arraystuff, and it will handle the numbering for you.

Upvotes: 0

Axel Amthor
Axel Amthor

Reputation: 11106

instead of

array_push($array, $add);

write

$array['item:1'] = $add;

Upvotes: 0

zerkms
zerkms

Reputation: 255015

Instead of array_push use this syntax:

$array['item:1'] = array("title" => "Revolution","size" => "100", "link" => "www");

This way you can specify the key name you want, whereas array_push just increments the numeric index.

Upvotes: 1

Related Questions