【发布时间】:2013-09-08 12:58:19
【问题描述】:
在 PHP 中,我有一个数组 $test。运行var_dump($test) 看起来像这样:
array(2) {
[0]=>
object(stdClass)#2 (6) {
["name"]=>
string(45) "Lorem"
["title"]=>
string(96) "Lorem ipsum"
}
[1]=>
object(stdClass)#3 (6) {
["name"]=>
string(41) "Ipsum"
["title"]=>
string(86) "Dolor sit amet"
}
}
现在我需要将另一个字段 (url) 添加到 $test 对象,所以它看起来像:
array(2) {
[0]=>
object(stdClass)#2 (6) {
["name"]=>
string(45) "Lorem"
["title"]=>
string(96) "Lorem ipsum"
["url"]=>
string(86) "http://www.google.com"
}
[1]=>
object(stdClass)#3 (6) {
["name"]=>
string(41) "Ipsum"
["title"]=>
string(86) "Dolor sit amet"
["url"]=>
string(86) "http://www.stackoverflow.com"
}
}
我尝试了foreach() 和$test->append('xxxxxxxx');,但出现错误。这不应该很容易做到吗?我做错了什么?
【问题讨论】:
-
用这个答案的建议替换附加:stackoverflow.com/questions/164395/…