PHP推动现有对象数组新的key和value [英] PHP push new key and value in existing object array
本文介绍了PHP推动现有对象数组新的key和value的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的研究对象和数组如何与PHP的工作,我有一个新的问题。现有的问题搜索并没有给自己的权利推。
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".
我有这样的例子:
$html_doc = (object) array
(
"css" => array(),
"js" => array()
);
array_push($html_doc , "title" => "testtitle");
这是为什么不工作?我是否需要先指定的关键称号?还是有另一种1号线的解决方案?
Why is this not working? Do i need to specify first the key title? Or is there another "1 line" solution?
推荐答案
array_push()不会允许你指定的键,只值:使用
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";
这篇关于PHP推动现有对象数组新的key和value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文