Parse JSON Array in php and add values to php array -
i have json object of 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 take values inside food , quantity , store them inside php array, ve tried many approaches no luck. point right way it, or wrong json message??
php json_decode's 2nd argument set true return associative arrays instead of objects.
additionaly, json valid food entry resolves string when using json_decode. in order have array want code snippet 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 php array manipulate @ 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 )
Comments
Post a Comment