dsfasdfsda234234 2018-06-19 03:32
浏览 124
已采纳

如何将数组中的字符串转换为数组并将它们组合在一起?

I have an array which look like this :

Result 1:

{
 "error_code": 0,
 "error_message": "",
 "return_data": {
    "items": [
        {
            "id": 462,
            "users": "1,36,38"
        },
        {
            "id": 462,
            "users": "1,4"
        },...... //same for 20 result
    ]
 }
}

I want the users to convert to an array,and separate by the comma,so the whole result will look like this :

The result I want:

{
    "error_code": 0,
    "error_message": "",
    "return_data": {
        "items": [
            {
                "id": 462,
                "users": [
                    {
                        "user_id": 1
                    },
                    {
                        "user_id": 36
                    },
                    {
                        "user_id": 38
                    }
                ],
            }.. //other result
        ]
    }
}

Here is what I try :

$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;
}

After I assign the array to $res with this line of code $res['return_data']['items']['users'] = $usersArray; ,my result become like below,I state the problem inside the code :

{
"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"
        }

    }
  }
}

So my question is,how can I convert a String in an array to an array,assign value to the key,and make it inside the array?

Somebody please help..Thanks

  • 写回答

1条回答 默认 最新

  • dounuogong0358 2018-06-19 04:01
    关注

    Like the comments above, you could have done it the first time so you wouldn't need another structure conversion, but anyway, your code is already there a bit, its just you need to create another nesting since you want another level:

    So you need another level here:

    "users": [
        {
            "user_id": 1
        },
        {
            "user_id": 36
        },
        {
            "user_id": 38
        }
    ],
    

    So in the code, just add []. This translates into:

    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;
    }
    

    Here's the fiddle to check it out.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化