Reputation: 1993
I seemed to be stuck with JSON Decode , i dont know how to decode the json object or maybe im doing something very wrong , iam doing :
$error_fields_structure['product_id'] = $this->input->post('product_id');
$error_fields_structure['main_product_quantity'] = $this->input->post('quantity');
$error_fields_structure = json_encode($error_fields_structure);
i pass $error_fields_structure to my view and in my java script i do:
<?php print_r(json_decode($error_fields_structure)); ?>;
i get an error in firebug and following output
stdClass Object
(
[product_id] => 62
[product_quantity] => 65
);
but if i do
<?php print_r(json_decode($error_fields_structure['product_id'])); ?>;
it gives me a null string and an error how do i get particular product_id and product_quantity from json object $error_fields_structure
Upvotes: 0
Views: 154
Reputation: 43265
You can only decode a valid json string.
<?php print_r(json_decode($error_fields_structure['product_id'])); ?>;
is incorrect as $error_fields_structure['product_id'] is not a json string.
Try this :
<?php
$errorFieldsArr = json_decode($error_fields_structure,true); //convert json string to array
var_dump($errorFieldsArr['product_id']); // get element from array
?>
or
<?php
$errorFieldsArr = json_decode($error_fields_structure); //convert json string to stdobject
var_dump($errorFieldsArr->product_id ); // get element from object
?>
Upvotes: 3