PHP 计数 JSON 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19606094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP count JSON array
提问by Buneme Kyakilika
I have searched SO but couldn't find an answer.My PHP script is receiving some JSON by http post that looks like this:
我已经搜索过,但找不到答案。我的 PHP 脚本正在通过 http 帖子接收一些 JSON,如下所示:
{
"task": [
{
"task_id": "3",
"task_due": "Oct 26 11:25",
"task_completed": "FALSE",
"task_desc": "fff",
"task_time": "20131026_112531",
"task_name": "fff"
},
{
"task_id": "2",
"task_due": "Oct 26 11:25",
"task_completed": "FALSE",
"task_desc": "rff",
"task_time": "20131026_112522",
"task_name": "xff"
},
{
"task_id": "1",
"task_due": "Oct 26 11:25",
"task_completed": "FALSE",
"task_desc": "fggg",
"task_time": "20131026_112516",
"task_name": "ff"
}
]}
As you can see, there are 3 items, but when I turn it into a PHP array object and count the items, I'm returned 1, when it should be 3, here is my PHP code:
正如你所看到的,有 3 个项目,但是当我把它变成一个 PHP 数组对象并计算这些项目时,我返回了 1,当它应该是 3 时,这是我的 PHP 代码:
$json_tasks = $_POST["json_array"];
$task_array = json_decode($json_tasks,true);
echo count($task_array);
And echo count
prints out '1' not '3'.
并echo count
打印出“1”而不是“3”。
回答by Oswald
Try echo count($task_array['task']);
尝试 echo count($task_array['task']);
In general, if you wonder what the structure of the value of a variable $var
is, do a
通常,如果您想知道变量值的结构是什么$var
,请执行以下操作
<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.
在替代品,如这个功能的优势serialize
,并print_r
是,它打印的PHP代码(并因此被人谁了解PHP(这很可能是,如果你在PHP程序)可读)。缺点var_export
是,它不能处理循环结构(例如 if $a->b == $a
),但 JSON 也不能。
回答by coltphp
$task_array = json_decode($json_tasks);
count($task_array->task);
EX: 3
例如:3
From Taiwan
来自台湾
回答by Deepanshu Goyal
Well the 3
items are in 1
item "task"
so, you have one array named task and the 3 elements are in it
那么3
项目在1
项目中,"task"
所以你有一个名为 task 的数组,其中有 3 个元素
try
尝试
echo count($task_array['task']);
EDIT :
编辑 :
please use the below code to print the array in correct pattern
请使用以下代码以正确的模式打印数组
echo '<pre>';
print_r($task_array['task']);
exit();
回答by Karthick
try this code, here i can able to count the number of objects with specific value
试试这个代码,在这里我可以计算具有特定值的对象的数量
here's my data.json file content
这是我的 data.json 文件内容
{"likes":[
{"user_id":1,"time":"12:04pm"},
{"user_id":2,"time":"02:04pm"},
{"user_id":67,"time":"11:04pm"},
{"user_id":1,"time":"12:04pm"}
]}
here's the php code
这是php代码
<?php
$jsonData = file_get_contents("data.json");
$data = json_decode($jsonData,true);
$total = 0;
foreach ($data["likes"] as $value) {
if($value["user_id"]==1){
$total = $total+1;
}
}
echo $total;
?>
output will be
输出将是
2