如何解析以下php数组

时间:2016-04-04 20:04:21

标签: php arrays

我正在尝试解析下面的php数组,不知怎的,我得到的结果,但我无法获得预期的输出。

阵列:

 Array (
        [0] => Array (
                [countNos] => 2
                [question_id] => 1
                [question] => Is Service Best?
                [rating] => 4
            )       
        [1] => Array (
                [countNos] => 1
                [question_id] => 2
                [question] => How much you benifitted?
                [rating] => 5
            )       
        [2] => Array (
                [countNos] => 1
                [question_id] => 2
                [question] => How much you benifitted?
                [rating] => 2               
            )
    )

当前代码:

foreach ($ratings as $rating) {

    if (!in_array($rating['question_id'], $ratingArr)) {
        $ratingArr[$rating['question_id']]['question_id'] = $rating['question_id'];
        $ratingArr[$rating['question_id']]['question'] = $rating['question'];
    }

    for ($i = 5; $i >= 1; $i--) {

        if (!in_array($rating['rating'], $ratingArr[$rating['question_id']]['stars'])) {
            if ($i == $rating['rating']) {
                $ratingArr[$rating['question_id']]['stars'][$i] = $rating['countNos'];
            } 
        }
    }

}

这是我得到的输出:

  

注意:未定义的索引:第153行的C:\ xampp \ htdocs \ mibs \ module \ Survey \ src \ Survey \ Service \ SurveyService.php中的星标

     

警告:in_array()期望参数2为数组,在第153行的C:\ xampp \ htdocs \ mibs \ module \ Survey \ src \ Survey \ Service \ SurveyService.php中给出null

Array (
    [1] => Array (
            [question_id] => 1
            [question] => Is Service Best?
            [stars] => Array (
                    [4] => 2
                )   
        )   
    [2] => Array (
            [question_id] => 2
            [question] => How much you benifitted?
            [stars] => Array (
                    [5] => 1
                    [2] => 1
                )
        )   
)

但我期待以下输出:

Array(
        [1] => Array(
                [question_id] => 1
                [question] => Is Service Best?
                [stars] => Array(
                        [5] => 0
                        [4] => 2
                        [3] => 0
                        [2] => 0
                        [1] => 0
                    )       
            )        
        [2] => Array(
                [question_id] => 2
                [question] => How much you benifitted?
                [stars] => Array(
                        [5] => 1
                        [4] => 0
                        [3] => 0
                        [2] => 1
                        [1] => 0
                    )        
            )        
    )

我如何解析这个数组,我总是遇到这种类型的问题,每当我解析时,我怎么能克服这个问题。

2 个答案:

答案 0 :(得分:3)

初始化新条目时,您永远不会在stars中创建$ratingArr子数组,这会导致两个警告。要获得所有零条目,您应该将其初始化为5个零的数组。然后你不需要循环来添加stars,你可以直接填写相应的条目。

您需要使用array_key_exists而不是in_array来检查$rating['question_id']中是否已有$ratingArr的条目。

foreach ($ratings as $rating) {
    if (!array_key_exists($rating['question_id'], $ratingArr)) {
        $ratingArr[$rating['question_id']]['question_id'] = $rating['question_id'];
        $ratingArr[$rating['question_id']]['question'] = $rating['question'];
        $ratingArr[$rating['question_id']]['stars'] = array_fill(1, 5, 0);
    }
    $ratingArr[$rating['question_id']]['stars'][$rating['rating']] = $rating['countNos'];
}

答案 1 :(得分:2)

循环遍历数组并检查结果数组中是否已有问题ID的subArray。如果没有,请初始化subArray。然后只需添加评级。

代码:

<?php

    $result = [];

    foreach($array as $v){
        if(!isset($result[$v["question_id"]])){
            $result[$v["question_id"]] = [
                    "question_id" => $v["question_id"],
                    "question" => $v["question"],
                    "stars" => array_fill_keys(range(5, 1), 0),
                ];
        }

        $result[$v["question_id"]]["stars"][$v["rating"]] += $v["countNos"]

    }

?>