如何使用PHP计算类似的数组键?

时间:2016-11-25 06:45:10

标签: php count array-key

foreach ($data as $key => $value) {
     $count = count(preg_grep("/^options(\d)+$/", array_keys(get_object_vars($value))));
     echo $count; exit;

echo $count给出了0.

如果我print_r($value);exit;,则$value变量会给出此结果:

Maatwebsite\Excel\Collections\CellCollection Object
(
    [title:protected] => 
    [items:protected] => Array
        (
            [question_set] => Computer Basics
            [question] => Who are You ?
            [options1] => RK
            [options2] => KAMAL
            [options3] => DPK
            [options4] => NARENDRA
            [marks] => 5
            [negative_marks] => 1
            [type] => 1
        )

)

我想计算键选项1,选项2等,因为这些选项是动态的。我怎么能这样做?

0 个答案:

没有答案