大家好,我有一个像下面这样的对象数组。我只想将新对象添加到当前数组中。任何帮助如何做到这一点。感谢
Array
(
[0] => stdClass Object
(
[sm_id] => 1
[c_id] => 1
)
[1] => stdClass Object
(
[sm_id] => 1
[c_id] => 2
)
)
输出应为
Array
(
[0] => stdClass Object
(
[sm_id] => 1
[c_id] => 1
)
[1] => stdClass Object
(
[sm_id] => 1
[c_id] => 2
)
[2] => stdClass Object
(
[sm_id] => 1
[c_id] => 3
)
)
答案 0 :(得分:3)
试试这个,
$object = new stdClass();
$object->sm_id = "1";
$object->c_id = "3";
$myArray[] = $object;
(或)
$myArray[] = (object) array('sm_id' => '1','c_id'=>'3');
答案 1 :(得分:2)
以下是您可以尝试的解决方案
$object = new ClassName();
$object->name = "Some_value";
$myArray[] = $object;
答案 2 :(得分:1)
您可以使用array_merge()
$array = array((object)array('sm_id' => 1,'c_id' => 1),(object)array('sm_id' => 1,'c_id' => 2));//Your object array;
$myarry[] = array('sm_id' => 1,'c_id' => 3); // Additional Array
$finalarr = (object) array_merge((array)$array, (array)$myarry);
答案 3 :(得分:1)
试一试:
<?php
//your default array
$your_array = array(0=> (object) array("sm_id"=>1, "c_id"=>1), 1=>(object) array("sm_id"=>1, "c_id"=>2));
//add object in your array
array_push($your_array, (object) array("sm_id"=>1, "c_id"=>3));
//show list
print_r($your_array);
?>