0

i have php array like (( print_r($fdata); ))

Array
(
    [status] => YES
    [data] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [0] => Need
                            [1] => Am need
                        )
                )
            [1] => Array
                (
                    [0] => Array
                        (
                            [0] => 0
                            [1] => No deductibles and no copayments.
                        )
                    [1] => Array
                        (
                            [0] => 1
                            [1] => No lifetime limit—policy won't terminate based on number or dollar amount of claims paid.
                        )
                )
            [2] => Array
                (
                    [0] => Array
                        (
                            [0] => Volvo
                            [1] => 22
                            [2] => 18
                        )

                    [1] => Array
                        (
                            [0] => Volvo
                            [1] => 22
                            [2] => 18
                        )
                )
            [3] => Array
                (
                    [0] => Array
                        (
                            [0] => Volvo
                            [1] => 22
                            [2] => 18
                        )
                    [1] => Array
                        (
                            [0] => Volvo
                            [1] => 22
                            [2] => 18
                        )
                )
        )
)

i want to encode it to json , but when i pass it to "json_encode" function it print blank result ! about my php code i have declared few php array in which i load data from database

$fdata = array();   // final data to process
$product = array();  // product info
$adv_arr = array();  // advantages
$benefits_arr = array();  // benefits
$limits_arr = array();  // limits
$terms_arr = array();  // terms

after loading to arrays , i push them to one another array like

  $ffdata = array();
  array_push($ffdata , $product ,$adv_arr,$benefits_arr,$terms_arr);    

and then it end i try to encode but no result

  $fdata['status'] = "YES";
              $fdata['data'] = $ffdata;
     echo json_encode($fdata);   

am trying to produce json data result like :: http://s1.postimg.org/efvvx74xr/C29_CA6_EA8_CAA946_E44076_CA72_B98502932_BA2_A6_DE4517_FB.jpg

sample data :: http://www.datafilehost.com/d/58c5d154

5
  • 1
    try to print $fdata using print_r($fdata) and check if it is empty or not.
    – Priyank
    Aug 14, 2015 at 11:47
  • above array which i printed is actually $fdata , which mean its not empty Aug 14, 2015 at 11:49
  • try to do print_r(json_encode($fdata));
    – Ilanus
    Aug 14, 2015 at 11:50
  • 1
    try after removing this string limit—policy won't
    – Priyank
    Aug 14, 2015 at 12:33
  • ya special character was causing problem - Aug 14, 2015 at 13:00

1 Answer 1

4

If json_encode() encounters an error when encoding a data set, it returns false, which will not echo.

In order to determine if the operation was erroneous, you can use json_last_error() and json_last_error_msg() to determine what went wrong.

For example:

$json = json_encode($data);

if (json_last_error() !== JSON_ERROR_NONE) {
    throw new RuntimeException(json_last_error_msg());
}

This should provide some more meaningful information if something goes wrong.

At this point, if you are getting a WSOD instead of any output then you might have a different problem; e.g: a fatal or parse error. In this case you should ensure that error reporting is enabled while developing (and, crucially, ensure it's disabled in your production environment!).

Generally, the easiest way to do this is to add the following to the top of your script:

<?php

error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', true);

Hope this helps :)

4
  • Hi. Look at the JSON error codes. 5 represents JSON_ERROR_UTF8 . it looks like you have malformed UTF8 data. For a more meaningful, human-readable error message use echo json_last_error_msg();. Aug 14, 2015 at 12:01
  • Uncaught exception 'RuntimeException' with message 'Malformed UTF-8 characters, possibly incorrectly encoded' in line # Aug 14, 2015 at 12:02
  • Indeed. You need to look at your data, that's where the problem is. Are you extracting it from a database or somewhere else? If you want to update your question to provide an example of this data we might be able to debug further. At the moment we can only determine the why there's a problem, not what :) Aug 14, 2015 at 12:05
  • looks like json cant encode special characters like — Aug 14, 2015 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.