0

I have a json object of this type:

{
    "order": {
        "Food": "[Test 1, Test 2, Test 0, Test 3, Test 1, Test 3, Test 11, Test 7, Test 9, Test 8, Test 2]",
        "Quantity": "[2, 3, 6, 2, 1, 7, 10, 2, 0, 0, 1]"
    },
    "tag": "neworder"
}

I have used json_decode but i would like to take the values inside Food and Quantity and store them inside a php array, i ve tried many approaches but really with no luck. Could someone point to the right way to do it, or is something wrong with my json message??

7
  • 1
    Can you show some of the code you used? Commented Jul 17, 2013 at 18:42
  • It seems that the "Food" and "Quality" values are themselves json strings Commented Jul 17, 2013 at 18:47
  • foreach($parsedJSON->order->Food as $mydata) { $response["test2"] = $mydata; } Commented Jul 17, 2013 at 18:47
  • And also precise what you exactly want to put in each element of the array. Commented Jul 17, 2013 at 18:47
  • yes thats correct, Explosion Pills Commented Jul 17, 2013 at 18:48

2 Answers 2

2

PHP json_decode's 2nd argument set to true will return associative arrays instead of objects.

Additionaly, your JSON is valid but your Food entry resolves to a string when using json_decode. In order to have the array you want this code snippet will work:

<?php
$json  = '{"order":{"Food":"[Test 1, Test 2, Test 0, Test 3, Test 1, Test 3, Test 11, Test 7, Test 9, Test 8, Test 2]","Quantity":[2,3,6,2,1,7,10,2,0,0,1]},"tag":"neworder"}';
$array = json_decode($json, true);

// Fix Food array entry
$array['order']['Food'] = explode(', ', trim($array['order']['Food'], '[]'));

print_r($array);

This way you'll get a PHP array to manipulate at will:

Array
(
    [order] => Array
        (
            [Food] => Array
                (
                    [0] => Test 1
                    [1] => Test 2
                    [2] => Test 0
                    [3] => Test 3
                    [4] => Test 1
                    [5] => Test 3
                    [6] => Test 11
                    [7] => Test 7
                    [8] => Test 9
                    [9] => Test 8
                    [10] => Test 2
                )

            [Quantity] => Array
                (
                    [0] => 2
                    [1] => 3
                    [2] => 6
                    [3] => 2
                    [4] => 1
                    [5] => 7
                    [6] => 10
                    [7] => 2
                    [8] => 0
                    [9] => 0
                    [10] => 1
                )
        )
    [tag] => neworder
)
Sign up to request clarification or add additional context in comments.

1 Comment

thnx very much, worked great, although i think i must organise my JSON in a different way so that it is easier to handle
0

If this:

{
    "order": {
        "Food": "[Test 1, Test 2, Test 0, Test 3, Test 1, Test 3, Test 11, Test 7, Test 9, Test 8, Test 2]",
        "Quantity": "[2, 3, 6, 2, 1, 7, 10, 2, 0, 0, 1]"
    },
    "tag": "neworder"
}

is truely the json that you are using then you are going to have to do a little work to get what you want.

$obj = json_decode($json);
// the food and quantity properties are string not json.
$foods = explode("," trim($obj->order->Food;, "[]"));
$foods = array_map("trim", $foods); // get rid of the extra spaces
$quantitys = json_decode($obj->order->Quantity);

For this to have been valid json it would have to be authored like

{
    "order": {
        "Food": ["Test 1", "Test 2", "Test 0", "Test 3", "Test 1", "Test 3", "Test 11", "Test 7", "Test 9", "Test 8", "Test 2"],
        "Quantity": [2, 3, 6, 2, 1, 7, 10, 2, 0, 0, 1]
    },
    "tag": "neworder"
}

Comments

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.