zahid ullah
zahid ullah

Reputation: 371

php json encode not encoding array

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

Upvotes: 0

Views: 2339

Answers (1)

Darragh Enright
Darragh Enright

Reputation: 14136

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

Upvotes: 4

Related Questions