0

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

1 Answer 1

2

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);

Sign up to request clarification or add additional context in comments.

5 Comments

what is better for preformance? this is the main page and going to happen alot!, use -> or let json,true fix it to array?
The performance difference is negligible. (And if you cared about performance you wouldn't be using PHP anyway.) I think you should use associative arrays because you never have to worry about key names which might be difficult or impossible to represent in PHP objects.
ok, "if i cared about performance", what would i use? :), (will let me accept your answare in 5 min.)
Francis, would you mind explaining whats the performance alternative to php?
Really? Anything with JIT compilation, anything that compiles down to machine code, heck, anything that doesn't completely toss the environment between each request. PHP by default doesn't even cache parsed bytecodes. There are many much more important considerations than saving a dozen milliseconds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.