Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将数组中的字符串转换为数组并将两者组合在一起?_Php_Arrays - Fatal编程技术网

Php 如何将数组中的字符串转换为数组并将两者组合在一起?

Php 如何将数组中的字符串转换为数组并将两者组合在一起?,php,arrays,Php,Arrays,我有一个如下所示的数组: 结果1: { "error_code": 0, "error_message": "", "return_data": { "items": [ { "id": 462, "users": "1,36,38" }, { "id": 462, "users": "1,4" },...... //sam

我有一个如下所示的数组:

结果1:

{
 "error_code": 0,
 "error_message": "",
 "return_data": {
    "items": [
        {
            "id": 462,
            "users": "1,36,38"
        },
        {
            "id": 462,
            "users": "1,4"
        },...... //same for 20 result
    ]
 }
}
我希望
用户
转换为数组,并用逗号分隔,因此整个结果如下所示:

我想要的结果:

{
    "error_code": 0,
    "error_message": "",
    "return_data": {
        "items": [
            {
                "id": 462,
                "users": [
                    {
                        "user_id": 1
                    },
                    {
                        "user_id": 36
                    },
                    {
                        "user_id": 38
                    }
                ],
            }.. //other result
        ]
    }
}
$res = "array the get the Result 1";
$items = //"I get the items array"

foreach ($items as $key => $item) {
    $usersArray = array(); //create a new Array

    //spilt the string to array separate with ","
    $usersIdArray = explode(',', $items['users']);

    //assign the "user_id" key to each value 
    foreach ($userIdArray as $key => $user) {

        $usersArray['user_id'] = $user;

    }
    //assign the result back to $res
    $res['return_data']['items']['users'] = $usersArray;
}
以下是我的尝试:

{
    "error_code": 0,
    "error_message": "",
    "return_data": {
        "items": [
            {
                "id": 462,
                "users": [
                    {
                        "user_id": 1
                    },
                    {
                        "user_id": 36
                    },
                    {
                        "user_id": 38
                    }
                ],
            }.. //other result
        ]
    }
}
$res = "array the get the Result 1";
$items = //"I get the items array"

foreach ($items as $key => $item) {
    $usersArray = array(); //create a new Array

    //spilt the string to array separate with ","
    $usersIdArray = explode(',', $items['users']);

    //assign the "user_id" key to each value 
    foreach ($userIdArray as $key => $user) {

        $usersArray['user_id'] = $user;

    }
    //assign the result back to $res
    $res['return_data']['items']['users'] = $usersArray;
}
在我使用这行代码将数组分配给$res之后,
$res['return\u data']['items']['users']=$usersArray,我的结果如下,我在代码中说明问题:

{
"error_code": 0,
"error_message": "",
"return_data": {
    "items": {
        "0":{ <-- // here suddenly appear a number for each result 
            "id": 462,
            "users": "1,36,38" //here nothing change (I want the array appear here)
        },
        "1":{
            "id": 462,
            "users": "1,36,38"
        },
        "2":{
            "id": 462,
            "users": "1,36,38"
        },
        "users": { //the array appear here but not the position I want..and it only appears 1 time.
            "user_id": "38"
        }

    }
  }
}
{
“错误代码”:0,
“错误消息”:“”,
“返回数据”:{
“项目”:{

“0”:{像上面的注释一样,您可以第一次这样做,这样就不需要另一个结构转换,但无论如何,您的代码已经存在了一些,只是您需要创建另一个嵌套,因为您需要另一个级别:

因此,这里需要另一个级别:

"users": [
    {
        "user_id": 1
    },
    {
        "user_id": 36
    },
    {
        "user_id": 38
    }
],
因此,在代码中,只需添加
[]
。这可以转化为:

foreach ($items as $key => $item) {
    $usersArray['id'] = $item['id'];
    $usersArray['users'] = array(); //create a new Array
    $usersIdArray = explode(',', $item['users']);
    foreach ($usersIdArray as $key => $user) {
        $usersArray['users'][] = array('user_id' => $user); // push each batch of key pair "user_id" key and value "each exploded id"
        // another level     ^
    }
    $res['return_data']['items'][] = $usersArray;
}

这是检查它的方法。

如何获得第一个数组?如果您自己创建它,可能最好调整它的创建方式。@Qirel我无法再调整它的创建方式了。因为它仍然有很多东西。现在我只能拆分字符串使其成为数组。兄弟,通过使用您的代码,用户数组显示为相同级别的w对于
@ken查看此示例,非常直接@ken您可以忽略它,因为它不是used@ken真高兴这有帮助