-1

I have a JSON (Line A) as shown below in which I want to calculate the number of items in a JSON object array.

   $fp = fopen('../feeds/ptp-ess_landing.json', 'w');
    fwrite($fp, json_encode($output));
    fclose($fp);
    logActivity();

    if(file_exists('../feeds/ptp-ess_landing.json')){
    $data = json_decode(file_get_contents('../feeds/ptp-ess_landing.json'));
    }

    {"toggle_multi_tiles":["0","1"]}  // Line A

At this moment, it is 2. It can be three as well or sometimes 4.

This is what I have tried:

$data->toggle_multi_tiles.length;   // Line B

On doing echo $data->toggle_multi_tiles.length; it returns Arraylength which is not the right output I am getting.

Problem Statement:

I am wondering what changes I should make in the php code above at Line B so that it returns 2.

flash
  • 1,455
  • 11
  • 61
  • 132

1 Answers1

2

Use count to count the number of elements in PHP Array

count ( $data->toggle_multi_tiles ) ;
ascsoftw
  • 3,466
  • 2
  • 15
  • 23