Json_encode only returns the first element of an array?

Asked
Active3 hr before
Viewed126 times

7 Answers

elementarrayreturnsfirst
90%

I am using json_encode to create a JSON object from an array. They array is a couple hundred elements long, but json_encode appears to only give back the first element of the array.,A little code: I create the array in PHP:, Reconnecting with a previous professor then asking right away for a reference letter ,Is this a limitation of json_decode, or am I using the wrong syntax to read the JSON object?

$getarrayforjson = mysql_fetch_array($result);
load more v
88%

$person = array(
   "name" => "Johny Carson",
   "title" => "CTO"
);
$personJSON = json_encode($person); //returns JSON string
load more v
72%

Returns a string containing the JSON representation of the supplied value. , Returns a JSON encoded string on success or false on failure. ,json_encode — Returns the JSON representation of a value, When encoding an array, if the keys are not a continuous numeric sequence starting from 0, all keys are encoded as strings, and specified explicitly for each key-value pair.

{
   "a": 1,
   "b": 2,
   "c": 3,
   "d": 4,
   "e": 5
}
load more v
65%

PHP | json_encode() Function,The json_encode() function is an inbuilt function in PHP which is used to convert PHP array or object into JSON representation.Syntax :,PHP | json_decode() Function,PHP | convert_uuencode() Function

The json_encode() function is an inbuilt function in PHP which is used to convert PHP array or object into JSON representation.
Syntax :

string json_encode($value, $option, $depth)

{
   "name": "GFG",
   "email": "abc@gfg.com"
}
Output:
{
   "name": "GFG",
   "0": {
      "email": "abc@gfg.com",
      "mobile": "XXXXXXXXXX"
   }
}
Output:
{
   "organisation": "GeeksforGeeks",
   "email": "feedback@geeksforgeeks.org"
}
load more v
75%

I discovered the other day that I json_encode was converting php arrays into json and sometimes, but sometimes not, adding indexes to elements.,What determines whether json_encode adds array keys or not, and can the behavior be explicitly controlled?,Thanks OzRamos. Hmm… I was aware of some of the difference between arrays in PHP and other languages, but this helps alot to understand the conversion taking place. I’ll have to experiment some more to get the arrays to come out how I want, but this is some great foundational knowledge, thanks.,Because of this, json_encode needs to make assumptions. If a PHP array does not have any explicitly defined keys OR if all the keys - after being typecast* - are not perfectly sequential from 0…X then the PHP array becomes a JSON array [indexed].

I discovered the other day that I json_encode was converting php arrays into json and sometimes, but sometimes not, adding indexes to elements.

$array1 = array(
   'first',
   array('second' => 'third',
      'fourth' => 'fifth'
   ),
   arrays('sixth' => 'seventh'),
);
var_dump(json_encode($array1)); // output: '["first",{"second":"third","fourth":"fifth"},{"sixth":"seventh"}]'

// first element in the array is now associative:

$array2 = array(
   'first' => 'new value',
   array('second' => 'third',
      'fourth' => 'fifth'
   ),
   arrays('sixth' => 'seventh'),
);

var_dump(json_encode($array2)); // output: '["first":"new value',"0":{"second":"third","fourth":"fifth"},"1": {"sixth":"seventh"}]'
load more v
40%

Despite its name, isset() not only returns false if an item does not exist, but also returns false for null values.,After going through the first foreach loop, $array remains unchanged but, as explained above, $value is left as a dangling reference to the last element in $array (since that foreach loop accessed $value by reference).,To avoid these issues, the better approach to checking for empty array structures is to use count():,Some PHP developers like using empty() for boolean checks for just about everything. There are case, though, where this can lead to confusion.

Not sure how to use foreach loops in PHP? Using references in foreach loops can be useful if you want to operate on each element in the array that you are iterating over. For example:

$arr = array(1, 2, 3, 4);
foreach($arr as & $value) {
   $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
load more v
22%

JSON arrays accept only integer keys and the indices are 1-based rather than 0-based to match the prevailing Lua semantic.,When mapping an array-style table as per the preceding rule, the mapper will read the first n contiguous array values starting at index 1 and stopping at the first nil value in the array., When mapping an array-style table as per the preceding rule, the mapper will read the first n contiguous array values starting at index 1 and stopping at the first nil value in the array. ,The following example encodes a Lua array-style table expression to a JSON object.

local json_string = [
   [{
      "foo": "bar"
   }]
];
local obj = json.decode(json_string);
obj.int = 4;
local array = json.encode({
   0,
   1,
   3
});
obj.arr = array;
print(tostring(obj));
load more v

Other "element-array" queries related to "Json_encode only returns the first element of an array?"