代码之家  ›  专栏  ›  技术社区  ›  ken

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

  •  0
  • ken  · 技术社区  · 7 年前

    我有一个这样的数组:

    结果1:

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

    我想要 users 要转换为数组并用逗号分隔,整个结果将如下所示:

    我想要的结果是:

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

    所以我的问题是,如何转换 String 在数组中,为键赋值,并将其放入数组中?

    有人帮忙……谢谢。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Kevin    7 年前

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

    所以你需要另一个层次:

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

    这是 fiddle 去看看。