2

I am trying to parse the following php array, somehow I am getting the result, but I couldn't get the expected output.

Array:

 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               
            )
    )

Current code:

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'];
            } 
        }
    }

}

This is the output I am getting:

Notice: Undefined index: stars in C:\xampp\htdocs\mibs\module\Survey\src\Survey\Service\SurveyService.php on line 153

Warning: in_array() expects parameter 2 to be array, null given in C:\xampp\htdocs\mibs\module\Survey\src\Survey\Service\SurveyService.php on line 153

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
                )
        )   
)

But I am expecting the following output:

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
                    )        
            )        
    )

How can I parse this array, I am always having this type of issue, whenever I am parsing, how can I overcome this.

2 Answers 2

3

You're never creating a stars sub-array in $ratingArr when you initialize a new entry, that's causing the two warnings. To get all the zero entries, you should initialize this to an array of 5 zeroes. Then you don't need a loop to add the stars, you can just fill in the appropriate entry directly.

And you need to use array_key_exists, not in_array, to check whether there's already an entry for $rating['question_id'] in $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'];
}
Sign up to request clarification or add additional context in comments.

3 Comments

Just looked at your code and asked why you used that for loop ;) Also !in_array($rating['question_id'], $ratingArr) does check if the question id exists as value in the $ratingArr but you use it as key, so you probably want to change that to isset() or just array_key_exsits(). And if the order of the keys in the stars subArray matters to OP, then you can either flip the array or use array_fill_keys()
Of course, what he actually meant was array_key_exists.
I'm going to assume for now that the order isn't significant.
2

Loop through your array and check if you already have a subArray with the question id in the result array. If not, initialize the subArray. And then just add the rating.

Code:

<?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"]

    }

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.