Jump to content

PHP JSON Encode


FooKelvin
Go to solution Solved by requinix,

Recommended Posts

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
	}]
}
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. "

Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.