如何在stdClass对象php中添加数组

时间:2013-05-09 10:00:42

标签: php arrays stdclass

我有一个这样的数组:

  

数组([0] => stdClass对象([id] => 1 [标题] => ABC [sentece] => blablabla ... [主题] =>科学)[1] = > stdClass对象([id] => 2 [title] => DEF [sentece] => nomnomnom ... [话题] =>技术)

如何在php中的stdClass对象中添加数组?

例如:我想添加这个数组:

$number = array (82, 61);

所以,我将有这个数组:

  

数组([0] => stdClass对象([id] => 1 [标题] => ABC [sentece] => blablabla ... [主题] =>科学 [数字] => 82 )[1] => stdClass对象([id] => 2 [title] => DEF [sentece] => nomnomnom ... [话题] =>技术   [number] => 61

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

根据您提供的信息,您需要手动执行此操作。

手动输入

$Array = [
    //your array
];
$Array[0]->number = 82;
$Array[1]->number = 61;

<强>自动化吗

$Array = [
    //your array
];
$Numbers = [82, 61];
foreach($Numbers as $key => $number) {
    if(isset($Array[$key]) {
        $Array[$key]->number = $number;
    }
}