简体   繁体   中英

How to calculate the array length in a JSON object array?

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.

使用count来计算PHP数组中的元素数

count ( $data->toggle_multi_tiles ) ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM