0

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

3 Answers 3

1

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.

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

Comments

0

instead of

array_push($array, $add);

write

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

Comments

0

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.

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.