1

I want to create an array of objects, and if some of them are duplicate,I want to increase some fields like (Votes/Score/Percentage), all good..in dump(I get the edited value,but on last dd ,the array is not modified. There I have all elements.

At the end I have 16 array elements (13 +3 with the same lkp_answer_id), insted of 13.

$history = AnswerHistory::where('question_id', '=', $id)->get();
$answers = $history->toArray();
$most_voted = [];
        foreach ($answers as $key => $answer) {
            if (!empty($most_voted)) {
                foreach ($most_voted as $best) {
                    //If value already exists, increase Votes/Score/Percentage
                    if ($best['answer_id'] == $answer['lkp_answer_id']) {
                        $best['Votes']++;
                        $best['Score'] = $best['Score'] + $answer['score'];
                        $best['Percentage'] = substr((($best['Votes'] * 100) / $query->votes), 0, 5);
                        $best['Weight'] = substr((($best['Score'] * 100) / $query->total_scoring), 0, 5);
                        dump($best);  //here the array element is changed (correct)
                    //Else add new array element
                    } else {
                        $most_voted[$key] = [
                            'answer_id' => $answer['lkp_answer_id'],
                            'Votes' => 1,
                            'Score' => $answer['score'],
                            'Percentage' => substr(((1 * 100) / $query->votes), 0, 5),
                            'Weight' => substr((($answer['score'] * 100) / $query->total_scoring), 0, 5),
                        ];
                    }
                }
            } else {
                //If $most_voted is null, insert first value
                $most_voted[$key] = [
                    'answer_id' => $answer['lkp_answer_id'],
                    'Votes' => 1,
                    'Score' => $answer['score'],
                    'Percentage' => substr(((1 * 100) / $query->votes), 0, 5),
                    'Weight' => substr((($answer['score'] * 100) / $query->total_scoring), 0, 5),

                ];
            }
        }

        dd($most_voted); //here I have a full list with, even duplicate ones
3
  • what is $answer ? where did you use $history ? Commented Apr 15, 2020 at 11:06
  • I edited the post, but I don't think that it matters. The problem is that the array element values are not changed at the end.. Commented Apr 15, 2020 at 11:09
  • Try my code and see it will work. Commented Apr 15, 2020 at 11:18

1 Answer 1

1

Change your code like below and try

$history = AnswerHistory::where('question_id', '=', $id)->get();
$answers = $history->toArray();
$most_voted = [];
        foreach ($answers as $key => $answer) {
            if (!empty($most_voted)) {
                foreach ($most_voted as $k => $v) {
                    //If value already exists, increase Votes/Score/Percentage
                    if ($most_voted[$k]['answer_id'] == $answer['lkp_answer_id']) {
                        $most_voted[$k]['Votes']++;
                        $most_voted[$k]['Score'] = $most_voted[$k]['Score'] + $answer['score'];
                        $most_voted[$k]['Percentage'] = substr((($most_voted[$k]['Votes'] * 100) / $query->votes), 0, 5);
                        $most_voted[$k]['Weight'] = substr((($most_voted[$k]['Score'] * 100) / $query->total_scoring), 0, 5);
                        dump($most_voted[$k]);  //here the array element is changed (correct)
                    //Else add new array element
                    } else {
                        $most_voted[$key] = [
                            'answer_id' => $answer['lkp_answer_id'],
                            'Votes' => 1,
                            'Score' => $answer['score'],
                            'Percentage' => substr(((1 * 100) / $query->votes), 0, 5),
                            'Weight' => substr((($answer['score'] * 100) / $query->total_scoring), 0, 5),
                        ];
                    }
                }
            } else {
                //If $most_voted is null, insert first value
                $most_voted[$key] = [
                    'answer_id' => $answer['lkp_answer_id'],
                    'Votes' => 1,
                    'Score' => $answer['score'],
                    'Percentage' => substr(((1 * 100) / $query->votes), 0, 5),
                    'Weight' => substr((($answer['score'] * 100) / $query->total_scoring), 0, 5),

                ];
            }
dd($most_voted); //here I have a full list with, even duplicate ones
Sign up to request clarification or add additional context in comments.

13 Comments

try again and give the dump and dd output.
I tried with something simple like " $best['Votes'] =12345 , but in the final dd the array is unchanged..
you try like my code and give the output. Like your above comment didn't work.
Here is the output: codeshare.io/5ZO7oe , the values are changed now, but I stii get duplicate answer_id ,in array..
Yes, but with duplicates, but is OK, for what I need next it doesn't bother me. Thanks a lot!
|

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.