You need to decode the json object and then count the elements in it ..
$json_array = json_decode($json_string, true);
$elementCount = count($json_array);
Answer from alwaysLearn on Stack OverflowYou need to decode the json object and then count the elements in it ..
$json_array = json_decode($json_string, true);
$elementCount = count($json_array);
You'll need to decode into PHP arrays before you do any work with the data.
Try:
$hugeArray = json_decode($huge, true); // Where variable $huge holds your JSON string.
echo count($hugeArray);
If you need to count to a lower depth, you'll need to iterate through the array.
For example, if you want to count the number of elements in the next layer, you can do:
foreach ($hugeArray as
value) {
echo $key.' - '.count($value);
}
However, this isn't necessarily meaningful because it depends on what you are trying to count, what your goal is. This block only counts the number of elements 1 layer underneath, regardless of what the actual numbers could mean.
php - How to get length of an array that is inside a json object? - Stack Overflow
PhP howto sizeof(JSON:array[]) - Stack Overflow
Calculate size in bytes of JSON payload including it in the JSON payload in PHP - Stack Overflow
php - Checking the size of a json file - Stack Overflow
slots is not an array, it's another object. If it were being serialized as an array it would probably look more like:
{ "slots":
[
{ "0": "1" },
{ "1": "0" },
{ "2": "0" },
...
]
}
Or even just:
{ "slots": [ "1", "0", "0" ] }
Try changing your loop to:
for (
i < $player_max_slots; $i++) { //player max slots comes from db
$invSlots['slots'][
inventory_info3[$i];
}
As Zimzat said in a comment above, once your array's indices start at 0 you should get an array of slots when you serialize your object to JSON.
Actually, according to some guy at the php.net forums, you need to be aware of index arrays.
<?php
echo json_encode(array("test","test","test"));
echo json_encode(array(0=>"test",3=>"test",7=>"test"));
?>
Will give :
["test","test","test"]
{"0":"test","3":"test","7":"test"}
Arrays are only returned if you don't define an index, or if your indexes are sequential and start at 0.
You've declared an associative array, not an indexed array. So you can't use length.
To get the count from an associative array, you need to iterate through its keys:
var count=0;
for (var key in $invSlots['slots'])
count++;
you can use this to calculate $content's size(DEMO):
$size = strlen(json_encode($content, JSON_NUMERIC_CHECK));
This will provide you with the whole length of json_encode()d string of $content. If you want to calculate the size in bytes(if using multibyte characters), this might be more helpful(DEMO):
$size = mb_strlen(json_encode($content, JSON_NUMERIC_CHECK), '8bit');
I guess this will be useful for many others, so I decided to answer to my own question using @mega6382 solution:
// prepare the JSON array
$JSONDATA=
array(
'response' => true,
'error' => null,
'payload' =>
array(
'content' => $content,
'size' => $size
)
);
// evaluate the JSON output size WITHOUT accounting for the size string itself
$t = mb_strlen(json_encode($JSONDATA, JSON_NUMERIC_CHECK), '8bit');
// add the contribution of the size string and update the value
$JSONDATA['payload']['size']=$t+strlen($t);
// output the JSON data
$joutput=json_encode($JSONDATA,JSON_NUMERIC_CHECK);
echo $joutput;
The file_get_contents() function reads the content into a string. Once it's in memory you don't need to try and read it again.
if (isset($_POST)) {
// populate the json variable
$json = file_get_contents('php://input');
// if the variable is not empty
// save the content for later
if (!empty($json))
{
$file = 'links-'.rand(10000000,99999999).".txt";
file_put_contents('Links/' . trim($file), $json);
}
}
?>
the very simple solution is decode the json and you can now check if the decoded json is an empty array.
$json = file_get_contents('php://input');
$json = json_decode($json);// decode json`
if (empty($json)) {
// json is empty
}
echo mb_strlen(serialize((array)$arr), '8bit');
If I understood well your question, you need the size of each "block" subarray inside the main array.
You can do something like this:
$sizes = array();
foreach($returnedArray as $key => $content) {
$sizes[$key] = count($content);
}
The $sizes array will be an associative array which the various "block"s as keys and the size of the data as values.
Edit: after the edit of the question, if the data inside the innermost arrays are strings or integers you can use a function like this:
function getSize($arr) {
$tot = 0;
foreach($arr as $a) {
if (is_array($a)) {
$tot += getSize($a);
}
if (is_string($a)) {
$tot += strlen($a);
}
if (is_int($a)) {
$tot += PHP_INT_SIZE;
}
}
return $tot;
}
assuming to have only ASCII-encoded strings.