在数组位置添加数组元素

时间:2016-01-16 18:20:12

标签: php arrays json

如何将数组添加到数组位置:

类似于:

<?php
$newArr = array('email' => array("id" => "5678", "token" => "fghjk"));
$arr = array(
        "auth"=>
            array(
                'users'=>
                    array(
                        'id' =>"456yhjoiu",
                        'token' => "asdfghjkrtyui678"
                    )
                )
            );

somefunction($arr['auth'], $newArr);

我已尝试array_push(),但在'email'之前添加了零(0).~

我这样做是为了得到一个json输出,如下所示:

}
    "auth": {
        "users": {
            "id": "456yhjoiu",
            "token": "asdfghjkrtyui678"
        },
        "email": {
             "id": "5678",
             "token": "fghjk"
        }
    }
}

但我有这个输出:

{
    "auth": {
        "users": {
            "id": "456yhjoiu",
            "token": "asdfghjkrtyui678"
        },
        "0": {
            "email": {
                "id": "5678",
                "token": "fghjk"
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

$data = ['auth' => array_merge($arr['auth'], $newArr)];

或旧数组表示法&lt; = PHP5.3

$data = array('auth' => array_merge($arr['auth'], $newArr));