20

In my study how objects and arrays work with PHP I have a new problem. Searching in existing questions didn't give myself the right "push".

I have this for example:

$html_doc = (object) array
    (
    "css"   => array(),
    "js"    => array()
    );
array_push($html_doc , "title" => "testtitle");

Why is this not working? Do i need to specify first the key title? Or is there another "1 line" solution?

0

2 Answers 2

56

array_push() doesn't allow you to specify keys, only values: use

$html_doc["title"] = "testtitle";

.... except you're not working with an array anyway, because you're casting that array to an object, so use

$html_doc->title = "testtitle";
Sign up to request clarification or add additional context in comments.

Comments

2

You can simply use $html_doc["title"] = "testtitle";

Check this comment on the array_push manual page.

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.