Arrays CakePHP数组大小

Arrays CakePHP数组大小,arrays,cakephp,options,sizeof,Arrays,Cakephp,Options,Sizeof,我有一个CakePHP的下拉列表。这是下拉列表中我的数组的结构: <?php echo $this->form->input('dropdown', array( 'options' => array( 'Item 1' => 'Item 1', 'Item 2' => array( 'Sub-item 2.1' => 'Sub-item 2.1', '

我有一个CakePHP的下拉列表。这是下拉列表中我的数组的结构:

<?php
   echo $this->form->input('dropdown', array(
      'options' => array(
         'Item 1' => 'Item 1',
         'Item 2' => array(
            'Sub-item 2.1' => 'Sub-item 2.1',
            'Sub-item 2.2' => 'Sub-item 2.2'
         ),
         'Item 3' => 'Item 3',
         'Item 4' => array(
            'Sub-item 4.1' => 'Sub-item 4.1',
            'Sub-item 4.2' => 'Sub-item 4.2'
         ),
      ),
      'empty' => '--select one--',
   ));
?>

我想得到“选项”的数组大小,但我得到的是错误的大小。当我尝试调试数组时,我得到的大小为4(项目1、2、3、4),子项目被忽略

我需要结果大小为6(第1项,第2.1子项,第2.2子项,第3项,第4.1子项,第42子项)

这可能吗

function countRecursive($myArray){
   $count = 0;
   foreach($myArray as $arrayValue){
       if(is_array($arrayValue)){
           $count += countRecursive($arrayValue):
       } else {
           $count++;
       }
   }
   return $count;
}

然后执行countRecursive($options)

您可以使用php的sizeof()函数来执行此操作

$array = array(
         'Item 1' => 'Item 1',
         'Item 2' => array(
            'Sub-item 2.1' => 'Sub-item 2.1',
            'Sub-item 2.2' => 'Sub-item 2.2'
         ),
         'Item 3' => 'Item 3',
         'Item 4' => array(
            'Sub-item 4.1' => 'Sub-item 4.1',
            'Sub-item 4.2' => 'Sub-item 4.2'
             ),
);

echo sizeof($array, 1);  // here 1 is parameter for recursive count
链接

我想最后两个最适合你的要求

    $count = 0;
    foreach ($array as $type) {
        $count+= count($type);
    }
 echo $count;
function countArray($array){
    $count = 0;
        foreach ($array as $type) {
            $count+= count($type);
        }
return count;
}
$count = countArray($array);
echo $count;