dougu1952 2015-02-04 16:21
浏览 45
已采纳

PHP多维数组 - 计算实例数

I am struggling a bit with a multidimensional array in PHP. I am new to PHP so please be aware that I may come across as a bit of a noob.

What I am trying to achieve: I am using ExpressionEngine as my CMS, with Freeform Pro to record answers to a survey. The values of the answers are 1, 2, 3, 4 or 5. That all works fine. Then I am using a SQL query to pull that saved data out of the database. This is where it starts getting complicated. I need the average of each question in the survey. So the logical thing to do (I would think) is to pull the data for each question and put it in an array:

$tva_1 = array();
$tva_2 = array();
$tva_3 = array();
$tva_4 = array();

SELECT form_field_259, form_field_260, form_field_261, form_field_262 FROM exp_freeform_form_entries_13 WHERE status='open'

array_push($tva_1, ('{form_field_259}'));
array_push($tva_2, ('{form_field_260}'));
array_push($tva_3, ('{form_field_261}'));
array_push($tva_4, ('{form_field_262}'));

Keep in mind that $tva_1 may have 5 variables in it, for example: 2, 4, 3, 1, 4. The same goes for $tva_2 and so on.

So once I have assigned that data to the various tva_ arrays, I then need to put those four arrays into another array, namely $allNumberArray:

$allNumberArray = array($tva_1, $tva_2, $tva_3, $tva_4);

So now if I try print_r($allNumberArray); it shows me all the data is there and everything is working as it should.

Array
(
    [0] => Array
        (
            [0] => 3
            [1] => 5
            [2] => 2
            [3] => 5
            [4] => 3
        )

    [1] => Array
        (
            [0] => 3
            [1] => 2
            [2] => 4
            [3] => 3
            [4] => 3
        )

    [2] => Array
        (
            [0] => 5
            [1] => 4
            [2] => 3
            [3] => 1
            [4] => 3
        )

    [3] => Array
        (
            [0] => 4
            [1] => 2
            [2] => 5
            [3] => 1
            [4] => 3
        )

The problem is: I need to count the number of times each rating has been used. So for example, I can see from the above print_r that 5 is displayed 4 times, 4 is displayed 3 times, 3 is displayed 7 times, etc.. I have tried a count function such as the one below, but that only works in a single dimension array (I think):

$userStrongAgree = count(array_filter($allNumberArray, function ($agreementFive) { return $agreementFive == 5; }));

I have been stuck on this problem now for a day and a half, and my deadline is tomorrow afternoon, so I am starting to get a bit worried. I really would appreciate any help or just a pointer in the right direction.

Many thanks

  • 写回答

2条回答 默认 最新

  • dqyknf4423 2015-02-04 21:47
    关注

    try to push all items into uniquq array and count it with array_count_values() :

    <?php
    $array=$allNumberArray; //your Array
    
    $new_arr=array();
    foreach($array as $key=>$value){
        foreach ($value as $key2 => $value2) {
            array_push($new_arr,$value2);
            }
    }
    $last_arr=array_count_values($new_arr);
    foreach ($last_arr as $key3 => $value3) {
        echo $key3.' is '.$value3.' times<br>';
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值