3

I am using following code to decode json array

$json_contacts ='[{"addr_name":"1","addr_phone":"010-1111-1111"},{"addr_name":"2","addr_phone":"010-1111-1112"},{"addr_name":"3","addr_phone":"010-1111-1113"},{"addr_name":"4","addr_phone":"010-1111-1114"}]';

$contact = json_decode($json_contacts, true);
echo $json_contacts;
echo "\n";
echo $contact;
echo "\nTHE END";

I am getting following result

[{"addr_name":"1","addr_phone":"010-1111-1111"},{"addr_name":"2","addr_phone":"010-1111-1112"},{"addr_name":"3","addr_phone":"010-1111-1113"},{"addr_name":"4","addr_phone":"010-1111-1114"}]

THE END

Why is my JSON_DECODE not decoding the array properly?

3
  • turn on error reporting on php - php.net/manual/en/function.error-reporting.php Commented Aug 8, 2015 at 2:11
  • tried but no errors... Commented Aug 8, 2015 at 4:59
  • I have checked your code and shows warning in line "echo $contact;" , changed into "print_r($contact);" and it prints all array values. Commented Aug 8, 2015 at 5:02

1 Answer 1

1

Try this:

<?php
$json_contacts ='[{"addr_name":"1","addr_phone":"010-1111-1111"},{"addr_name":"2","addr_phone":"010-1111-1112"},{"addr_name":"3","addr_phone":"010-1111-1113"},{"addr_name":"4","addr_phone":"010-1111-1114"}]';

$contact = json_decode($json_contacts, true);
echo $json_contacts;
echo "\n";
echo '<pre>';
print_r( $contact);

echo '</pre>';
echo "\nTHE END";

Because you echo your previous $json_contacts and you didn't turn on PHP notice. You cannot echo $contact because it's an array.

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

2 Comments

I am not that much of newbie... Ofcourse the script is written with in <?php ?>. Or else it will not have shown the result as I mentioned above.
@HamDongKyun i know you're not newbie. please just copy all of that. and do print_r($contact); not echo $contact.

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.