Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多维数组计数_Php_Arrays_Count - Fatal编程技术网

Php 多维数组计数

Php 多维数组计数,php,arrays,count,Php,Arrays,Count,我有我的主阵列: array(6) { [1]=> array(3) { [0]=> string(15) "Extension" [1]=> int(1) [2]=> string(6) "3,00 " } [2]=> array(3) { [0]=> string(32) "Physics " [1]=> string(1) "1"

我有我的主阵列:

array(6) { 
    [1]=> array(3) { 
        [0]=> string(15) "Extension" 
        [1]=> int(1) 
        [2]=> string(6) "3,00 " 
    } 
    [2]=> array(3) { 
        [0]=> string(32) "Physics " 
        [1]=> string(1) "1" 
        [2]=> string(6) "3,00 " 
    } 
    [3]=> array(3) { 
        [0]=> string(31) "Physics " 
        [1]=> int(1) 
        [2]=> string(6) "6,00 " 
    } 
    [4]=> array(3) { 
        [0]=> string(34) "Desk" 
        [1]=> int(4) 
        [2]=> string(8) "127,00 " 
    } 
    [5]=> array(3) { 
        [0]=> string(18) "assistance" 
        [1]=> int(1) 
        [2]=> string(7) "12,50 " 
    } 
    [6]=> array(3) { 
        [0]=> string(15) "Extension" 
        [1]=> int(1) 
        [2]=> string(6) "3,00 " 
    } 
} 
我的预期产出是:

   Extension 2 
   Physics 2
   Desk 1 
   Assistance 1 
结果必须在resultarray中 我该怎么办?我试过使用array\u count\u values函数,但不起作用

我如何储存answear: 我试过这个代码,但不起作用

  $tabrecap = array();
  foreach($counts as $key=>$value){
   //echo $key." qte".$value;
  $tabrecap = array ($key,$value,$valueOption);
  }

只需做一个循环,并使用每个数组的第一项作为键:

$array = array(
    array("Extension", 1, "3,00"),
    array("Physics", "1", "3,00"),
    array("Physics", 1, "6,00 ")
);
$count = array();

foreach($array as $a)
    $count[$a[0]]++;

var_dump($count); // array(2) { ["Extension"]=> int(1) ["Physics"]=> int(2) }
循环就是答案

<?php
// untested

$counts = Array();
foreach( $array as $subArray ){
    $value = $subArray[0];
    $counts[ $value ] = ( isset($counts[ $value ]) )
        ? $counts[ $value ] + 1
        : 1;
}

var_dump( $counts);

如您在评论中所问,请尝试以下操作:-

<?php
$array =   array( '1'=> array('0'=>"Extension", '1'=> 1, '2'=>"3,00 " ), '2'=> array('0'=>"Physics",'1'=>"1","3,00 " ),'3'=> array('0'=>"Physics",'1'=>1,"6,00 "),'4'=> array('0'=>"Desk",'1'=>4,"127,00 "),'5'=> array('0'=>"assistance",'1'=>1,"12,50 " ),'6'=> array('0'=>"Extension",'1'=>1,"3,00 "));

$count = array();

$i = 0;

foreach ($array as $key=>$arr) {
  // Add to the current group count if it exists
  if (isset($count[$i][$arr[0]])) {
    $count[$i][$arr[0]]++;
  }
  else $count[$i][$arr[0]] = 1;

  $i++;
}
print_r($count);
 ?>


输出:-

您尝试过什么吗?可能会有帮助you@Lovemyfate我已经尝试过这个函数,但它是多维数组,所以不起作用:(您可以使用foreach和CountThank,这是我想要的,我尝试了foreach,但在索引中我错了您的代码将为
$count
ya中每个未定义的索引生成一个通知,它是生成此通知的,所以它是错误的,我很抱歉可以创建这样的数组:数组[0]=>{[0]=>扩展[1]=>2}{[0]=>Physics[1]=>2}……我想这不是OP在书中要求的question@Uchiha谢谢你的指点。但我认为这很容易追踪。但我也编辑了我的答案来摆脱它。谢谢你的建议。