i use JSON.stringify(pageSettings) in jquery to ajax an array to php and save the file. file content:
{"MidHeight":367,"BotTop":502}
i use json_decode to load it back to array in php:
$pageSettings=json_decode(file_get_contents($path.$file);
when i print_r($pageSettings,true) results are:
stdClass Object
(
[MidHeight] => 276
[BotTop] => 411
)
but when i try to read from it with:
$pageSettings["MidHeight"]
i get:
PHP Fatal error: Cannot use object of type stdClass as array.
Either use property-access notation ($pageSettings->MidHeight) or tell json_decode to always give you an associative array using the second argument: $pageSettings = json_decode($json_str, true);
Related
I am converting object to array using
$data['payment_cheque'] = (array)$data['payment_cheque'];
What i Get is
"\u0000*\u0000items": ["2017-04-18","2017-06-28","2017-07-05"]
What I want is
["2017-04-18","2017-06-28","2017-07-05"]
Just try this
$data['payment_cheque'] =json_decode(json_encode($data['payment_cheque'] ,true),true);
I have state list in JSON format and i am using json_decode function but when i am trying to access array values getting error.
$states='{
"AL": "Alabama",
"AK": "Alaska",
"AS": "American Samoa",
"AZ": "Arizona",
"AR": "Arkansas"
}';
$stateList = json_decode($states);
echo $stateList['AL'];
Fatal error: Cannot use object of type stdClass as array on line 65
You see, the json_decode() method doesn’t return our JSON as a PHP array; it uses a stdClass
object to represent our data. Let’s instead access our object key as an object attribute.
echo $stateList->AL;
If you provide true as the second parameter to the function we will receive our PHP array exactly
as expected.
$stateList = json_decode($states,true);
echo $stateList['AL'];
Either you pass true to json_decode like Nanhe said:
$stateList = json_decode($states, true);
or change the way you access it:
echo $stateList->{'AL'};
You could pass true as the second parameter to json_encode or explicitly convert the class to array. I hope that helps.
$stateList = json_decode($states);
//statelist is a object so you can not access it as array
echo $stateList->AL;
If you want to get an array as a result after decode :
$stateList = json_decode($states, true);
//statelist is an array so you can not access it as array
echo $stateList['AL'];
I'm trying to output the value of the email value of an array, but have problems doing so.
The array is based on json_decode()
This is the error I receive
Fatal error: Cannot use object of type stdClass as array in /home/.... line 57
JSON (value of: $this->bck_content)
{"email":"test#email.com","membership_id":"0","fname":"Kenneth","lname":"Poulsen","userlevel":"1","created":"2012-04-23 10:57:45","lastlogin":"2012-04-23 10:58:52","active":"y"}
My code
# Display requested user details
$details_array = json_decode($this->bck_content);
$value = $details_array['email'];
print $value;
You need to use the second argument to json_decode to force array structures on JS objects.
json_decode($this->bck_content, true);
This will make sure all JS objects in the json are decoded as associative arrays instead of PHP StdObjects.
Of course that is assuming you want to use array notation to access them. If you're fine with using object notation then you can just use:
$value = $details_array->email;
try this one
$value = $details_array->email;
or
json_decode($json, true);
or
$details_array = (array)json_decode($json);
what have you done wrong is writen in error description
A JSON array has the form:
[[a,b,c],[a,b,c],[a,b,c]]
Is there a better way than split?
No, this is most certainly not the best way to parse JSON. JSON parsers exist for a reason. Use them.
In JavaScript, use JSON.parse:
var input = '[[1,2,3],[1,2,3],[1,2,3]]';
var arrayOfArrays = JSON.parse(input);
In PHP, use json_decode:
$input = '[[1,2,3],[1,2,3],[1,2,3]]';
$arrayOfArrays = json_decode($input);
You do not need to use regular expressions. As has been mentioned, you must first have valid JSON to parse. Then it is a matter of using the tools already available to you.
So, given the valid JSON string [[1,2],[3,4]], we can write the following PHP:
$json = "[[1,2],[3,4]]";
$ar = json_decode($json);
print_r($ar);
Which results in:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 3
[1] => 4
)
)
If you want to decode it in JavaScript, you have a couple options. First, if your environment is new enough (e.g. this list), then you can use the native JSON.parse function. If not, then you should use a library like json2.js to parse the JSON.
Assuming JSON.parse is available to you:
var inputJSON = "[[1,2],[3,4]]",
parsedJSON = JSON.parse(inputJSON);
alert(parsedJSON[0][0]); // 1
In JavaScript , I think you can use Eval() → eval() method...
I am using $.getJSON() to pass some data to the server side (PHP, Codeigniter) and using the return data to do some work. The data that I am sending over to the server is in the form of an array.
Problem: When an associative array is sent to the server, no result is received on server side. However, if a normal array with numerical indexes is sent, the data is received on server side. How can I send an array of data over to the server?
JS Code (Not Working)
boundary_encoded[0]['testA'] = 'test';
boundary_encoded[0]['testB'] = 'test1';
$.getJSON('./boundary_encoded_insert_into_db_ajax.php',
{boundary_encoded: boundary_encoded},
function(json) {
console.log(json);
});
JS Code (Works)
boundary_encoded[0][0] = 'test0';
boundary_encoded[0][1] = 'test1';
$.getJSON('./boundary_encoded_insert_into_db_ajax.php',
{boundary_encoded: boundary_encoded},
function(json) {
console.log(json);
});
PHP Code
$boundary_encoded = $_GET['boundary_encoded'];
print_r($_GET);
Error Msg
<b>Notice</b>: Undefined index: boundary_encoded in <b>C:\xampp\htdocs\test\boundary\boundary_encoded_insert_into_db_ajax.php</b> on line <b>11</b><br />
Array
(
)
Working Result
Array
(
[boundary_encoded] => Array
(
[0] => Array
(
[0] => test
[1] => test1
)
)
)
The reason this isn't working is because JavaScript does not support associative arrays. This assignment:
boundary_encoded[0]['testA'] = 'test';
appears to work in JS because you can assign a new property to any object, arrays included. However they won't be enumerated in a for loop.
Instead, you must use an object literal:
boundary_encoded[0] = {'testA':'test'};
You can then use JSON.stringify to convert boundary_encoded to a JSON string, send that to the server, and use PHP's json_decode() function to convert the string back into an array of objects.
I would suggest using converting the arrays to JSON. If you can't do so in PHP (using the json_encode function), here are a couple of JS equivalents:
http://phpjs.org/functions/json_encode:457
http://www.openjs.com/scripts/data/json_encode.php
In your getJSON call, use
{boundary_encoded: JSON.stringify(boundary_encoded)},
instead of
{boundary_encoded: boundary_encoded},