2 回答
TA贡献1801条经验 获得超16个赞
尝试这个,
您不需要 array_push()。只需将数组添加$users['Users']到里面即可$list。注意:我已将array()符号更改为[]符号。
$list =[
"Version" => "1.0"
];
$users = [
'Users' => [
'Username' => 'test',
'Level' => '10'
]
];
if(!isset($list['Users'])) {
$list['Users'][] = $users['Users'];
echo json_encode($list);
}
exit;
输出:
{"Version":"1.0","Users":[{"Username":"test","Level":"10"}]}
TA贡献1818条经验 获得超8个赞
如果新键users不存在,您可以添加它,然后向其中添加元素,如下所示:
$list = array(
"Version" => "1.0"
);
$users = array(
"Users" => array(
"Username"=> "test",
"Level"=> "10"
)
);
if(!isset($list['Users'])) $list['users']=[];
foreach( $users as $a ){
$list['users'][]=$a;
}
echo json_encode( $list );
输出:
{"Version":"1.0","users":[{"Username":"test","Level":"10"}]}
- 2 回答
- 0 关注
- 142 浏览
添加回答
举报