Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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";
share|improve this answer

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

Check this comment on the array_push manual page.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.