0

I stored some values in db using json_encode.Now on fetching i got values like this ["ab","cd"].I have tried by exploding,json_encode and then decode.But nothing works.some of tried code is below

$array = "["ab","cd"]";
$value = (array)$array;

//-------------
$array = (array) $array;
// get_object_vars
$array = get_object_vars($object);
print_r($array);

when i loop directly on array i didn't get any values.Thanks for any help in advance. on this i got like this :

var_dump(json_decode($object));
print_r($object);

OUTPUT :

NULL ["MKD","KD3"]
5
  • $array = json_decode('["ab","cd"]'); Commented Jan 18, 2016 at 3:31
  • @RobbieAverill thanks but you can check my question that after decoding i get null.Have look now on my question Commented Jan 18, 2016 at 4:17
  • Please post a realistic example of your $array value (var_dump($array)). What you've posted has syntax errors so it's not much use. Commented Jan 18, 2016 at 4:25
  • @RobbieAverill May be but the print r showing the complete data.issue is with jscon decode.its inside variable so if i echo it print like ['abc','dde'].as for error concern i didn't get any error on log Commented Jan 18, 2016 at 4:32
  • I think i have to go and use str replace method. Commented Jan 18, 2016 at 4:34

1 Answer 1

2

If I am understanding your question, I think you are looking for json_decode.

$json_encoded_str = '["ab","cd"]';

// Will return an array of elements in your string  
var_dump(json_decode($json_encoded_str));

The result would be

array(2) {
    [0]=> string(2) "ab"
    [1]=> string(2) "cd"
}
Sign up to request clarification or add additional context in comments.

6 Comments

thanks for your message but when i try like that i got this reponse.check my question
It seems your object is not of type String. Try var_dump($object); and see if you get something like string(13) "["MKD","KD3"]" . If not, your $object is not json encoded.
when i var dump the value it prints like this string(33) "["MKD","KD3"]"
And when try with jsond_deconde it prints null.
i think its db issue.Its inside the db values is ["MKD","KD3"]
|

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.