Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP count JSON array

I have searched SO but couldn't find an answer.My PHP script is receiving some JSON by http post that looks like this:

{
"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:

$json_tasks = $_POST["json_array"];
$task_array = json_decode($json_tasks,true);
echo count($task_array);

And echo count prints out '1' not '3'.

like image 654
Buneme Kyakilika Avatar asked Oct 26 '13 11:10

Buneme Kyakilika


1 Answers

Well the 3 items are in 1 item "task" so, you have one array named task and the 3 elements are in it

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();
like image 131
Deepanshu Goyal Avatar answered Oct 11 '22 00:10

Deepanshu Goyal