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

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

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 SQL Server下载
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求