Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.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 - Fatal编程技术网

Php 计数多阵列速率

Php 计数多阵列速率,php,arrays,Php,Arrays,我数这个有困难。 我想计算属于id_图像的所有速率 可能像key=id\u image和value=tot count一样,我尝试了使用数组的值,但是当它的值为-S时,我不能正常使用它 Array ( [0] => Array ( [id_image] => 12 [rate] => 4 ) [1] => Array ( [id_image] => 13 [rate] =>

我数这个有困难。 我想计算属于id_图像的所有速率

可能像key=id\u image和value=tot count一样,我尝试了使用数组的值,但是当它的值为-S时,我不能正常使用它

Array
(
[0] => Array
    (
        [id_image] => 12
        [rate] => 4
    )

[1] => Array
    (
        [id_image] => 13
        [rate] => 4
    )

[2] => Array
    (
        [id_image] => 14
        [rate] => 3
    )

[3] => Array
    (
        [id_image] => 13
        [rate] => 4
    )

[4] => Array
    (
        [id_image] => 12
        [rate] => 5
    )

[5] => Array
    (
        [id_image] => 12
        [rate] => 4
    )

)
//测试数组
$arr=数组(
0=>数组(
“id_图像”=>1,
“速率”=>3
),
1=>数组(
“id_图像”=>2,
“速率”=>8
),
2=>数组(
“id_图像”=>3,
“速率”=>4
),
3=>数组(
“id_图像”=>1,
“速率”=>2
),
4=>数组(
“id_图像”=>3,
“速率”=>2
)
);
//将长度放在一个变量中,这样我们就不会一直调用count();
$length=计数($arr);
//保存速率总和的新数组
$totals=array();
//迭代测试数组
对于($i=0;$i<$length;++$i){
//检查$totals是否已包含指定id\u映像的数据
如果(isset($totals[$arr[$i]['id\u image']])){
//如果是,请添加数据
$totals[$arr[$i]['id_image']]+=$arr[$i]['rate'];
}否则{
//如果不是,则设置数据
$totals[$arr[$i]['id_image']]=$arr[$i]['rate'];
}
}
var_dump(总计);

我不明白。。。你想这么做吗<代码>计数($array[0]['id\u image'])还是计数所有id\u image?对于countall id_image,您可以执行一个循环:
For($k=0,$i=0;$k
// test array
$arr = array(
  0 => array(
    'id_image' => 1,
    'rate' => 3
  ),
  1 => array(
    'id_image' => 2,
    'rate' => 8
  ),
  2 => array(
    'id_image' => 3,
    'rate' => 4
  ),
  3 => array(
    'id_image' => 1,
    'rate' => 2
  ),
  4 => array(
    'id_image' => 3,
    'rate' => 2
  )
);

// put the length in a var so we don't keep calling count();
$length = count($arr);

// the new array that'll hold the sum of the rates
$totals = array();

// iterate through the test array
for ($i = 0; $i < $length; ++$i) {
  // check if $totals already contains data for the specified id_image
  if (isset($totals[$arr[$i]['id_image']])) {
    // if so, add data
    $totals[$arr[$i]['id_image']] += $arr[$i]['rate'];
  } else {
    // if not so, set data
    $totals[$arr[$i]['id_image']] = $arr[$i]['rate'];
  }
}

var_dump($totals);