Why You Can't array_push() Into an Object Property Directly
You cannot use array_push($html_doc, "title" => "testtitle") because you are attempting to push a value onto the root of the $html_doc object rather than into one of its specific properties. In PHP, when you cast an array to an object using (object), the resulting entity behaves like a map of properties where each key becomes a named attribute. The array_push() function operates on arrays by appending elements to the end of the index list. It expects a standard indexed array structure.
When you pass $html_doc as the first argument, PHP treats it as an object, not an array. Even though objects have properties that might contain arrays, array_push() does not automatically target those internal arrays based on key-value syntax provided in the second argument. The syntax "title" => "testtitle" inside the push call is interpreted incorrectly because array_push() only accepts a mix of values to append and an optional array offset or key (in some contexts) or simply ignores the key-value pair format intended for object construction.
To fix this, you must explicitly target the specific property where you want the new data stored. In your example, $html_doc is initialized with css and js as empty arrays. If your intention is to add a title to the root of the object, you are actually trying to create a new top-level property named title. However, since $html_doc is an object, you should assign values directly to its properties using the arrow operator (->).
Here is the correct way to add a new key-value pair to an existing object:
$html_doc = (object) array(
"css" => array(),
"js" => array()
);
// Correct approach for adding a new top-level property
$html_doc->title = "testtitle";
If your goal was to push the title onto one of the existing arrays (for instance, if you wanted $html_doc->css to contain the string), you would need to access that specific array first. You cannot mix object properties and array_push syntax in a single statement without explicitly accessing the array property:
// If 'title' was intended to be part of an existing array (e.g., css)
$html_doc->css[] = "testtitle";
// or
array_push($html_doc->css, "testtitle");
The confusion often stems from how array_push handles the second argument. When used with arrays, you can pass multiple values: array_push($arr, "a", "b"). If you try to pass an associative pair like "key" => "value", PHP attempts to treat that string as a literal value to be pushed, not as a definition for a new key in an array. Arrays use numeric indices by default unless explicitly constructed with keys. Objects, however, allow arbitrary string keys when assigned directly via the property operator.
If you are working within a Laravel context or handling complex data structures where this pattern appears frequently, consider using stdClass or Laravel's Eloquent models which handle attribute assignment more intuitively. For instance, if you are building a dynamic response structure, ensuring that you distinguish between indexed collections and associative objects early prevents runtime errors later when iterating over the data.
The fundamental rule here is: use ->key = value for adding new properties to objects, and use array_push() or $array[] only after explicitly accessing an array property within that object. Trying to force a key-value assignment into array_push() on an object root violates the separation between indexed array operations and associative object property assignment.