0

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

2
  • How do you get that first array? If you create it on your own, might be better to adjust how it's created. Commented Jun 19, 2018 at 3:43
  • @Qirel I cant adjust how it created anymore..Cause it still have a lot of thing..For now I only can split the string to make it become the array.. Commented Jun 19, 2018 at 3:46

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

bro by using your code,the users array appear the same level with items
@ken check out this sample, pretty straight forward 3v4l.org/IiJDY
@ken you can disregard it as it is not used
@ken sure glad this helped

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.