PHP Count JSON Array

PHP count JSON array

Try echo count($task_array['task']);

In general, if you wonder what the structure of the value of a variable $var is, do a

<pre><?php var_export($var, true); ?></pre>

Advantage of this function over alternatives such as serialize and print_r is, that it prints PHP code (and is thus readable by anyone who understands PHP (which is likely if you program in PHP)). Disadvantage of var_export is, that it cannot handle circular structures (e.g. if $a->b == $a), but neither can JSON.

How to count json element in PHP?

One approach

<?php
$data = json_decode($json, true);

echo count($data);
echo count($data[0]);

or another one


echo count($data);
foreach($data as $o){
echo count($o);
}

Count number of objects in JSON PHP?

Your JSON represents an object, not an array. In your object, you have like property which is an array so you need to write like this

count($jsonInPHP->like);

How to count the number of elements present in a JSON object?

You just need to make sure there is no empty values inside that array. Because when empty string is exploded it will create one element with value as blank.

Remove those values using array_filter

echo count(array_filter(explode(",", $data->posts_id_fr)));

// Will print 0

Just in case if you want to loop through as suggested by GrumpyCrouton

foreach($data as $key => $value)
{
echo $key . ' : ' . count(array_filter(explode(",", $value))) . PHP_EOL;
}

Output:
posts_id_en : 3
posts_id_fr : 0
reports_id_en: 2
reports_id_fr: 1

Count json object by finding its value in php

Decode it first then it will return then count the data leader.

Try this:

$decoded = json_decode($json, true);

$count = 0;
foreach($decoded as $key => $val)
{
if($val['leader'] == 1)
{
$count++;
}
}
echo $count;
//if you want also to count the false then add else in the statement echo $count++;
}

Count JSON values with PHP

First Ive used json formater to get a valid json array https://jsonformatter.curiousconcept.com/ Then you can try this:

//decode json array We receive multidimensional array
$t = json_decode('[
[
{
"id_piesa":"7",
"cantitate_piesa":1,
"garantie_piesa":false
},
{
"id_piesa":"18",
"cantitate_piesa":1,
"garantie_piesa":false
},
{
"id_piesa":"313",
"cantitate_piesa":1,
"garantie_piesa":false
},
{
"id_piesa":"312",
"cantitate_piesa":1,
"garantie_piesa":false
}
],
[
{
"id_piesa":"68",
"cantitate_piesa":1,
"garantie_piesa":false
},
{
"id_piesa":"44",
"cantitate_piesa":1,
"garantie_piesa":false
},
{
"id_piesa":"168",
"cantitate_piesa":1,
"garantie_piesa":true
},
{
"id_piesa":"444",
"cantitate_piesa":1,
"garantie_piesa":false
},
{
"id_piesa":"91",
"cantitate_piesa":1,
"garantie_piesa":false
}
],
[
{
"id_piesa":"168",
"cantitate_piesa":1,
"garantie_piesa":false
},
{
"id_piesa":"44",
"cantitate_piesa":1,
"garantie_piesa":false
},
{
"id_piesa":"308",
"cantitate_piesa":1,
"garantie_piesa":false
},
{
"id_piesa":"1",
"cantitate_piesa":1,
"garantie_piesa":false
},
{
"id_piesa":"27",
"cantitate_piesa":1,
"garantie_piesa":false
},
{
"id_piesa":"26",
"cantitate_piesa":1,
"garantie_piesa":false
},
{
"id_piesa":"65",
"cantitate_piesa":1,
"garantie_piesa":false
},
{
"id_piesa":"74",
"cantitate_piesa":1,
"garantie_piesa":true
}
],
[
{
"id_piesa":"82",
"cantitate_piesa":1,
"garantie_piesa":false
}
],
[
{
"id_piesa":"120",
"cantitate_piesa":1,
"garantie_piesa":false
}
],
[
{
"id_piesa":"120",
"cantitate_piesa":1,
"garantie_piesa":true
}
],
[
{
"id_piesa":"71",
"cantitate_piesa":1,
"garantie_piesa":false
}
],
[
{
"id_piesa":"168",
"cantitate_piesa":1,
"garantie_piesa":false
},
{
"id_piesa":"44",
"cantitate_piesa":1,
"garantie_piesa":false
},
{
"id_piesa":"91",
"cantitate_piesa":1,
"garantie_piesa":false
},
{
"id_piesa":"444",
"cantitate_piesa":1,
"garantie_piesa":false
}
]
]',true);

//This will be our new array with statistic data
$newArr = [];

//Looping arrays
foreach($t as $s) {
foreach($s as $arr) {
// We'll use id_piesa for keys in our $newArr, here we check if record
// with this key exists in the new array
if(!array_key_exists($arr['id_piesa'], $newArr))
{
// if not create new record and assign cantitate_piesa
$newArr[$arr['id_piesa']] = [
'id' => $arr['id_piesa'],
'cantitate_piesa_sum' => (int)$arr['cantitate_piesa']
];
// check if garantie_piesa is TRUE or FALSE and count that
// for this record
if($arr['garantie_piesa'] == true) {
$newArr[$arr['id_piesa']]['countTrue'] = 1;
} else {
$newArr[$arr['id_piesa']]['countFalse'] = 1;
}
} else {
// record exists so just add to it cantitate_piesa
// from array being looped
$newArr[$arr['id_piesa']]['cantitate_piesa_sum'] =
$newArr[$arr['id_piesa']]['cantitate_piesa_sum'] + (int)$arr['cantitate_piesa'];
// again check if garantie_piesa is TRUE or FALSE and count that
// for this record
if($arr['garantie_piesa'] == true) {
$newArr[$arr['id_piesa']]['countTrue'] = $newArr[$arr['id_piesa']]['countTrue']+1;
} else {
$newArr[$arr['id_piesa']]['countFalse'] = $newArr[$arr['id_piesa']]['countFalse']+1;
}
}
}
}

echo "<pre>";
var_dump($newArr);
echo "</pre>";

Count the length of JSON array and access the json element in PHP

Have you tried a different url/file? I coppied your code exact and it works 100%.

Make sure the php://input is correct. Try store the JSON in an external file and try link to that file.

EDIT

try this:

$json = file_get_contents('something.json');
$arr = json_decode($json, true);
$count = 0;
for($i = 0; $i < count($arr['data']); $i++) {
$count++;
}
echo $count;

code

result

There you can see the exact code, and output. As you can see, the same JSON is being output, and then the count under it.

If you're still not getting it right, then you're using a different JSON source, or you have something overriding the variables in PHP



Related Topics



Leave a reply



Submit