我正在尝试创建包含几个数组的新 stdclass,然后将其转换为 json 数组,例如我有这个数组:
$my_arr = array (name=>myname1, adress=>myadd1, phone=>myphone1);
$my_arr2 = array (name=>myname2, adress=>myadd2, phone=>myphone2);
我想将它们合并到 STDClass,这是我尝试过的:
$foo = new stdClass();
$foo->item1 = array();
foreach ($my_arr as $key => $value) {
$foo->item1[$key] = $value;
}
print_r($foo);
echo json_encode($foo);
这里的问题是我只使用第一个数组,结果应该是:
"items":[
[
{
"name":"myname1",
"adress":"myadd1",
"phone":"myphone1"
},
{
"name":"myname2",
"adress":"myadd2",
"phone":"myphone2"
},
{
"name":"myname3",
"adress":"myadd3",
"phone":"myphone3"
}
]
],
非常感谢!