FooKelvin Posted December 15, 2016 Share Posted December 15, 2016 Hi All, Here is my code, trying to encode the result from database. $stmt = sqlsrv_query($conn, $result); $data = array(); while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { $data[] = $row; } //echo json_encode( $data ); $out = array(); foreach($data as $element) { $out[$element['category']][] = array($element['trainFunction']=> $element['value1']); } echo json_encode( $out ); Result of JSON Echo: {"November":[{"A":7},{"B":8},{"C":3},{"D":4}],"December":[{"A":1},{"B":2},{"C":1}]} Expected Output: { "November": [{ "A": 7, "B": 8, "C": 3, "D": 4 }], "December": [{ "A": 1, "B": 2, "C": 1 }] } Quote Link to comment https://forums.phpfreaks.com/topic/302745-php-json-encode/ Share on other sites More sharing options...
FooKelvin Posted December 15, 2016 Author Share Posted December 15, 2016 Update Expected Output: [{ "A": 7, "B": 8, "C": 3, "D": 4, "category": "November" }, { "A": 1, "B": 2, "C": 1, "category": "December" }] Quote Link to comment https://forums.phpfreaks.com/topic/302745-php-json-encode/#findComment-1540354 Share on other sites More sharing options...
requinix Posted December 15, 2016 Share Posted December 15, 2016 Start by building { "November": { "A": 7, "B": 8, "C": 3, "D": 4, "category": "November" }, "December": { "A": 1, "B": 2, "C": 1, "category": "December" } }You're close to that already: $out[$element['category']][] = array($element['trainFunction']=> $element['value1']);Move the trainFunction value into the empty [] to specify a key, then remove the array() part so that it's just value1. Then use array_values() on $out to strip off the outermost keys and turn it into a regular array. Quote Link to comment https://forums.phpfreaks.com/topic/302745-php-json-encode/#findComment-1540356 Share on other sites More sharing options...
FooKelvin Posted December 15, 2016 Author Share Posted December 15, 2016 Thanks Requinix, I have change to foreach($data as $element) { $out[$element['category']][$element['trainFunction']] = $element['value1']; } Output: {"November":{"Engineering":7,"Leadership & Soft Skills":8,"Lean":3,"Quality":4},"December":{"Engineering":2,"Leadership & Soft Skills":3,"Quality":1}} But i do not understand, "use array_values() on $out to strip off the outermost keys and turn it into a regular array. " Quote Link to comment https://forums.phpfreaks.com/topic/302745-php-json-encode/#findComment-1540365 Share on other sites More sharing options...
Solution requinix Posted December 15, 2016 Solution Share Posted December 15, 2016 You also need a line of code that adds the month category into the array. Outside of the loop. $out[$element['category']]['category'] = $element['category'];Take a look at the documentation for array_values. Do you see what it does to arrays? It strips off the custom keys and replaces them with regular numeric keys. You need to do that to your array before you json_encode() it - those custom keys vs numeric keys are what makes the difference between getting {} objects or [] arrays in the JSON. Quote Link to comment https://forums.phpfreaks.com/topic/302745-php-json-encode/#findComment-1540369 Share on other sites More sharing options...
FooKelvin Posted December 16, 2016 Author Share Posted December 16, 2016 You also need a line of code that adds the month category into the array. Outside of the loop. $out[$element['category']]['category'] = $element['category'];Take a look at the documentation for array_values. Do you see what it does to arrays? It strips off the custom keys and replaces them with regular numeric keys.You need to do that to your array before you json_encode() it - those custom keys vs numeric keys are what makes the difference between getting {} objects or [] arrays in the JSON. Yes Requinix, Thanks for the explanation, its really helps me! Quote Link to comment https://forums.phpfreaks.com/topic/302745-php-json-encode/#findComment-1540424 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.