PHP Json_Encode Not Working on Arrays Partially

php json_encode not working on arrays partially

You should make sure that every component of your web application uses UTF-8. This answer from another question will tell you how to do this. If you read this from RFC4627:

Encoding: JSON text SHALL be encoded in Unicode. The default encoding is UTF-8.

The json_encode function requires all incoming data to be UTF-8 encoded. That's why strings such as Åland Islands are probably causing you problems.

json_encode doesn't display all array values

Try below and you'll get to know what is happening exactly:

$json = json_encode($response);

if ($json)
echo $json;
else
echo json_last_error_msg();

json_last_error_msg() - Returns the error string of the last json_encode() or json_decode() call

json_encode will not encode more than 6670 rows

Possibly depends on your PHP version and the memory allowed for use by PHP (and possibly the actual size of all the data in the array, too).
What you could do if all else fails, is this:
Write a function that checks the size of a given array, then split off a smaller part that will encode, then keep doing this until all parts are encoded, then join them again. If this is the route you end up taking, post a comment on this answer, and perhaps I can help you out with it. (This is based on the answer by Nytrix)
EDIT, example below:

function encodeArray($array, $threshold = 6670) {
$json = array();
while (count($array) > 0) {
$partial_array = array_slice($array, 0, $threshold);
$json[] = ltrim(rtrim(json_encode($partial_array), "]"), "[");
$array = array_slice($array, $threshold);
}

$json = '[' . implode(',', $json) . ']';
return $json;
}

json_encode function not return Braces {} when array is empty in php

use the JSON_FORCE_OBJECT option of json_encode:

json_encode($status, JSON_FORCE_OBJECT);

Documentation

JSON_FORCE_OBJECT (integer)
Outputs an object rather than an array when a non-associative array is used. Especially useful when the recipient of the output is expecting an object and the array is empty. Available since PHP 5.3.0.

Or, if you want to preserve your "other" arrays inside your object, don't use the previous answer, just use this:

$status = array(
"message"=>"error",
"club_id"=>$_club_id,
"status"=>"1",
"membership_info"=> new stdClass()
);

json_encode empty with no error

It is because your class's properties are private.

An example class with only private properties ...

php > class Foo { private $bar = 42; }
php > $obj = new Foo();

do not expose values:

php > echo json_encode($obj);
{}

But an example class with public properties ...

php > class Bar { public $foo = 42; }
php > $objBar = new Bar();

do it!

php > echo json_encode($objBar);
{"foo":42}

\JsonSerializable

PHP provide an'interafce \JsonSerializable that require a method jsonSerialize. This method is automatically called by json_encode().

class JsonClass implements JsonSerialize {
private $bar;
public function __construct($bar) {
$this->bar = $bar;
}
public function jsonSerialize() {
return [
'foo' => $this->bar,
];
}
}

I prefer this solution because is not good to expose publicly properties

serialization and unserialization ...

If you need to serialize and unserialize php object you can ...

php > class Classe { public $pub = "bar"; }
php > $obj = new Classe();
php > $serialized = serialize($obj);
php > $original = unserialize($serialized);
php > var_dump($original);
php shell code:1:
class Classe#2 (1) {
public $pub =>
string(3) "bar"
}

$serialized variable contains O:6:"Classe":1:{s:3:"pub";s:3:"bar";}. As you can see is not a json, but is a format that allow you to recreate original object using unserialize function.

json encode returning only partial data

You're looking at two different JSON data structures. One is an array, the other an object. Essentially, json_encode() will use the former where possible, and switch to objects if the data can't be represented by an array.

Change json_encode($model_data); to json_encode($model_data, JSON_FORCE_OBJECT); to make json_encode() always return an object, which will have the keys you want.

Further example:

$data = array(34,26,25,23,6);

echo json_encode($data); // [34,26,25,23,6]

echo json_encode($data, JSON_FORCE_OBJECT); // {"0":34,"1":26,"2":25,"3":23,"4":6}


Related Topics



Leave a reply



Submit