PHP JSON Encode not working
This is the var_dump of the array I want to encode into JSON:
array(3) {
[0]=> array(2) {
["From"]=> string(14) "08 August 2013"
["To"]=> string(14) "21 August 2013"
}
[1]=> array(2) {
["From"]=> string(14) "11 August 2013"
["To"]=> string(14) "21 August 2013"
}
[2]=> array(2) {
["From"]=> string(14) "12 August 2013"
["To"]=> string(14) "01 August 2013"
}
}
When I encode it, the output looks like this:
[
{"From":"08 August 2013","To":"21 August 2013"},
{"From":"11 August 2013","To":"21 August 2013"},
{"From":"12 August 2013","To":"01 August 2013"}
]
But I want it to be this:
{
0:{"From":"08 August 2013","To":"21 August 2013"},
1:{"From":"11 August 2013","To":"21 August 2013"},
2:{"From":"12 August 2013","To":"01 August 2013"}
}
It's possible because I've done it before, but using the same code now it won't work
php arrays json encode var-dump
add a comment |
This is the var_dump of the array I want to encode into JSON:
array(3) {
[0]=> array(2) {
["From"]=> string(14) "08 August 2013"
["To"]=> string(14) "21 August 2013"
}
[1]=> array(2) {
["From"]=> string(14) "11 August 2013"
["To"]=> string(14) "21 August 2013"
}
[2]=> array(2) {
["From"]=> string(14) "12 August 2013"
["To"]=> string(14) "01 August 2013"
}
}
When I encode it, the output looks like this:
[
{"From":"08 August 2013","To":"21 August 2013"},
{"From":"11 August 2013","To":"21 August 2013"},
{"From":"12 August 2013","To":"01 August 2013"}
]
But I want it to be this:
{
0:{"From":"08 August 2013","To":"21 August 2013"},
1:{"From":"11 August 2013","To":"21 August 2013"},
2:{"From":"12 August 2013","To":"01 August 2013"}
}
It's possible because I've done it before, but using the same code now it won't work
php arrays json encode var-dump
Tryjson_encode($array, JSON_FORCE_OBJECT)
. Not sure if it will work though, honestly.
– Phas1c
Aug 2 '13 at 14:21
add a comment |
This is the var_dump of the array I want to encode into JSON:
array(3) {
[0]=> array(2) {
["From"]=> string(14) "08 August 2013"
["To"]=> string(14) "21 August 2013"
}
[1]=> array(2) {
["From"]=> string(14) "11 August 2013"
["To"]=> string(14) "21 August 2013"
}
[2]=> array(2) {
["From"]=> string(14) "12 August 2013"
["To"]=> string(14) "01 August 2013"
}
}
When I encode it, the output looks like this:
[
{"From":"08 August 2013","To":"21 August 2013"},
{"From":"11 August 2013","To":"21 August 2013"},
{"From":"12 August 2013","To":"01 August 2013"}
]
But I want it to be this:
{
0:{"From":"08 August 2013","To":"21 August 2013"},
1:{"From":"11 August 2013","To":"21 August 2013"},
2:{"From":"12 August 2013","To":"01 August 2013"}
}
It's possible because I've done it before, but using the same code now it won't work
php arrays json encode var-dump
This is the var_dump of the array I want to encode into JSON:
array(3) {
[0]=> array(2) {
["From"]=> string(14) "08 August 2013"
["To"]=> string(14) "21 August 2013"
}
[1]=> array(2) {
["From"]=> string(14) "11 August 2013"
["To"]=> string(14) "21 August 2013"
}
[2]=> array(2) {
["From"]=> string(14) "12 August 2013"
["To"]=> string(14) "01 August 2013"
}
}
When I encode it, the output looks like this:
[
{"From":"08 August 2013","To":"21 August 2013"},
{"From":"11 August 2013","To":"21 August 2013"},
{"From":"12 August 2013","To":"01 August 2013"}
]
But I want it to be this:
{
0:{"From":"08 August 2013","To":"21 August 2013"},
1:{"From":"11 August 2013","To":"21 August 2013"},
2:{"From":"12 August 2013","To":"01 August 2013"}
}
It's possible because I've done it before, but using the same code now it won't work
php arrays json encode var-dump
php arrays json encode var-dump
edited Aug 2 '13 at 14:24
Android Developer
9451822
9451822
asked Aug 2 '13 at 14:18
Josh Luke BleaseJosh Luke Blease
56711853
56711853
Tryjson_encode($array, JSON_FORCE_OBJECT)
. Not sure if it will work though, honestly.
– Phas1c
Aug 2 '13 at 14:21
add a comment |
Tryjson_encode($array, JSON_FORCE_OBJECT)
. Not sure if it will work though, honestly.
– Phas1c
Aug 2 '13 at 14:21
Try
json_encode($array, JSON_FORCE_OBJECT)
. Not sure if it will work though, honestly.– Phas1c
Aug 2 '13 at 14:21
Try
json_encode($array, JSON_FORCE_OBJECT)
. Not sure if it will work though, honestly.– Phas1c
Aug 2 '13 at 14:21
add a comment |
4 Answers
4
active
oldest
votes
Use the : JSON_FORCE_OBJECT argument;
$json = json_encode($array,JSON_FORCE_OBJECT);
That will assign numeric keys
also casting the array to object (json_encode((object)$array);
) works too. underlying arrays wont be forced to object this way. this might be preferable in some cases
– x4rf41
Aug 2 '13 at 14:25
add a comment |
Both versions are equivalent. JS by default will number the array elements starting with 0, so even though they're not specified, you'll still get 0,1,2,.... as the indexes when you decode later on.
You can trivially verify this by decoding the string, e.g.
$array = array('your array here');
$json = json_encode($array);
$decoded_array = json_decode($json);
var_dump($decoded_array);
add a comment |
if you want those index because you want an array of json objects, you don´t need them because you already have that ,if you want to select any of those objects inside of the array you could already use index like:
example[0].from;
Every {} means an json object and [{},{},{}] means an array of objects.
add a comment |
Many Times it occurs when our content of the array is not Encoded. Generally we use UTF-8 Encoding.
So, To Solve this issue Just add
mysqli_set_charset($con, 'utf8');
Just after the connection.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18019203%2fphp-json-encode-not-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use the : JSON_FORCE_OBJECT argument;
$json = json_encode($array,JSON_FORCE_OBJECT);
That will assign numeric keys
also casting the array to object (json_encode((object)$array);
) works too. underlying arrays wont be forced to object this way. this might be preferable in some cases
– x4rf41
Aug 2 '13 at 14:25
add a comment |
Use the : JSON_FORCE_OBJECT argument;
$json = json_encode($array,JSON_FORCE_OBJECT);
That will assign numeric keys
also casting the array to object (json_encode((object)$array);
) works too. underlying arrays wont be forced to object this way. this might be preferable in some cases
– x4rf41
Aug 2 '13 at 14:25
add a comment |
Use the : JSON_FORCE_OBJECT argument;
$json = json_encode($array,JSON_FORCE_OBJECT);
That will assign numeric keys
Use the : JSON_FORCE_OBJECT argument;
$json = json_encode($array,JSON_FORCE_OBJECT);
That will assign numeric keys
answered Aug 2 '13 at 14:23
JohnnyFaldoJohnnyFaldo
2,81521326
2,81521326
also casting the array to object (json_encode((object)$array);
) works too. underlying arrays wont be forced to object this way. this might be preferable in some cases
– x4rf41
Aug 2 '13 at 14:25
add a comment |
also casting the array to object (json_encode((object)$array);
) works too. underlying arrays wont be forced to object this way. this might be preferable in some cases
– x4rf41
Aug 2 '13 at 14:25
also casting the array to object (
json_encode((object)$array);
) works too. underlying arrays wont be forced to object this way. this might be preferable in some cases– x4rf41
Aug 2 '13 at 14:25
also casting the array to object (
json_encode((object)$array);
) works too. underlying arrays wont be forced to object this way. this might be preferable in some cases– x4rf41
Aug 2 '13 at 14:25
add a comment |
Both versions are equivalent. JS by default will number the array elements starting with 0, so even though they're not specified, you'll still get 0,1,2,.... as the indexes when you decode later on.
You can trivially verify this by decoding the string, e.g.
$array = array('your array here');
$json = json_encode($array);
$decoded_array = json_decode($json);
var_dump($decoded_array);
add a comment |
Both versions are equivalent. JS by default will number the array elements starting with 0, so even though they're not specified, you'll still get 0,1,2,.... as the indexes when you decode later on.
You can trivially verify this by decoding the string, e.g.
$array = array('your array here');
$json = json_encode($array);
$decoded_array = json_decode($json);
var_dump($decoded_array);
add a comment |
Both versions are equivalent. JS by default will number the array elements starting with 0, so even though they're not specified, you'll still get 0,1,2,.... as the indexes when you decode later on.
You can trivially verify this by decoding the string, e.g.
$array = array('your array here');
$json = json_encode($array);
$decoded_array = json_decode($json);
var_dump($decoded_array);
Both versions are equivalent. JS by default will number the array elements starting with 0, so even though they're not specified, you'll still get 0,1,2,.... as the indexes when you decode later on.
You can trivially verify this by decoding the string, e.g.
$array = array('your array here');
$json = json_encode($array);
$decoded_array = json_decode($json);
var_dump($decoded_array);
answered Aug 2 '13 at 14:21
Marc BMarc B
315k31323426
315k31323426
add a comment |
add a comment |
if you want those index because you want an array of json objects, you don´t need them because you already have that ,if you want to select any of those objects inside of the array you could already use index like:
example[0].from;
Every {} means an json object and [{},{},{}] means an array of objects.
add a comment |
if you want those index because you want an array of json objects, you don´t need them because you already have that ,if you want to select any of those objects inside of the array you could already use index like:
example[0].from;
Every {} means an json object and [{},{},{}] means an array of objects.
add a comment |
if you want those index because you want an array of json objects, you don´t need them because you already have that ,if you want to select any of those objects inside of the array you could already use index like:
example[0].from;
Every {} means an json object and [{},{},{}] means an array of objects.
if you want those index because you want an array of json objects, you don´t need them because you already have that ,if you want to select any of those objects inside of the array you could already use index like:
example[0].from;
Every {} means an json object and [{},{},{}] means an array of objects.
answered Aug 2 '13 at 14:27
Jao AssyJao Assy
246
246
add a comment |
add a comment |
Many Times it occurs when our content of the array is not Encoded. Generally we use UTF-8 Encoding.
So, To Solve this issue Just add
mysqli_set_charset($con, 'utf8');
Just after the connection.
add a comment |
Many Times it occurs when our content of the array is not Encoded. Generally we use UTF-8 Encoding.
So, To Solve this issue Just add
mysqli_set_charset($con, 'utf8');
Just after the connection.
add a comment |
Many Times it occurs when our content of the array is not Encoded. Generally we use UTF-8 Encoding.
So, To Solve this issue Just add
mysqli_set_charset($con, 'utf8');
Just after the connection.
Many Times it occurs when our content of the array is not Encoded. Generally we use UTF-8 Encoding.
So, To Solve this issue Just add
mysqli_set_charset($con, 'utf8');
Just after the connection.
answered Nov 22 '18 at 6:38
Dharmesh PatelDharmesh Patel
12
12
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18019203%2fphp-json-encode-not-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try
json_encode($array, JSON_FORCE_OBJECT)
. Not sure if it will work though, honestly.– Phas1c
Aug 2 '13 at 14:21