Php 具有多个级别的laravel对象项中的项之和

Php 具有多个级别的laravel对象项中的项之和,php,laravel,Php,Laravel,我想拿一个名为“投票计数”的项目,把它们加起来,看看选民总数。对象非常大,我将删除不相关的内容,只留下重要部分 { "id":1, "name":"Evaluacion al docente por parte de los estudiantes", "description":"Evaluacion al docente de la clase de Electronica 1 de la carrera de Ingenieria", "user_id":1,

我想拿一个名为“投票计数”的项目,把它们加起来,看看选民总数。对象非常大,我将删除不相关的内容,只留下重要部分

{ 
   "id":1,
   "name":"Evaluacion al docente por parte de los estudiantes",
   "description":"Evaluacion al docente de la clase de Electronica 1 de la carrera de Ingenieria",
   "user_id":1,
   "anonymous":0,
   "created_at":"2019-12-09 10:30:50",
   "updated_at":"2019-12-10 08:43:40",
   "result":[ 
      { 
         "id":1,
         "type":1,
         "rank":null,
         "title":"Cual es su opinion acerca de la oficina de telematica",
         "options":{ 
            "1":{ 
               "title":"answer question 1",
               "vote_count":null
            },
            "7":{ 
               "title":"answer question 1",
               "vote_count":null
            },
            "13":{ 
               "title":"answer question 1",
               "vote_count":null
            },
            "19":{ 
               "title":"answer question 1",
               "vote_count":null
            }
         }
      },
      { 
         "id":2,
         "type":2,
         "rank":null,
         "title":"elija 3 opciones",
         "options":{ 
            "1":{ 
               "title":"opcion a",
               "vote_count":1
            },
            "2":{ 
               "title":"opcion b",
               "vote_count":0
            },
            "3":{ 
               "title":"opcion c",
               "vote_count":1
            },
            "4":{ 
               "title":"opcion d",
               "vote_count":2
            }
         }
      },
      { 
         "id":5,
         "type":1,
         "rank":null,
         "title":"texto aslkfjasd asd afsdf",
         "options":{ 
            "3":{ 
               "title":"jdflaksfj alkdfj ajfawieur alk jaklf",
               "vote_count":null
            },
            "9":{ 
               "title":"sdf asd ads",
               "vote_count":null
            },
            "15":{ 
               "title":"sdfa sd fasdfads a",
               "vote_count":null
            },
            "21":{ 
               "title":"asdfasd",
               "vote_count":null
            }
         }
      },
      { 
         "id":3,
         "type":4,
         "rank":null,
         "title":"bota la basura en los basureros?",
         "options":{ 
            "1":{ 
               "title":"No",
               "vote_count":2
            },
            "2":{ 
               "title":"Si",
               "vote_count":1
            }
         }
      },
      { 
         "id":4,
         "type":3,
         "rank":6,
         "title":"Que calificacion le daria a la limpieza general de la universidad?",
         "options":{ 
            "1":{ 
               "title":1,
               "vote_count":0
            },
            "2":{ 
               "title":2,
               "vote_count":1
            },
            "3":{ 
               "title":3,
               "vote_count":0
            },
            "4":{ 
               "title":4,
               "vote_count":1
            },
            "5":{ 
               "title":5,
               "vote_count":0
            },
            "6":{ 
               "title":6,
               "vote_count":1
            }
         }
      },
      { 
         "id":6,
         "type":3,
         "rank":3,
         "title":"jaksjakjf fjf a jaskj fkf ja range",
         "options":{ 
            "1":{ 
               "title":1,
               "vote_count":1
            },
            "2":{ 
               "title":2,
               "vote_count":1
            },
            "3":{ 
               "title":3,
               "vote_count":1
            }
         }
      }
   ],

   //rest of it, that is not relevant
}
正如你所见,人们选择的问题和选项是我试图计算的,以便稍后显示选择问题选项而不是数量的人的百分比,因为有些问题是可选的,我必须计算每个问题的总数,而不是整个调查。这就是我在控制器中保存数据的方式。我将删除文本类型问题保存部分,因为该类型问题不需要总计

$survey = Survey::with('surveyQuestions.responseType',
            'surveyQuestions.surveyQuestionOption', 'surveyQuestions.answer')
            ->where('id', $id)->first();

$result = [];

foreach ($survey['surveyQuestions'] as $question) {
    $options = [];

    //multiple choice
    if ($question->response_type_id === 2)  {
        foreach ($question['surveyQuestionOption'] as $option) {
            $count = 0;
            foreach ($survey['answer'] as $answer) {
                if ($answer->survey_question_id === $option->survey_question_id
                        && (int)$answer->answer === $option->id) {
                    $count++;
                }

                $options[$option->id] = [
                    'title' => $option->option,
                    'vote_count' => $count
                ];
            }
        }
    }

    //ranking
    if ($question->response_type_id === 3) {
        if ($question->rank === 3){
            $question->opciones = [
                ['id' => 1, 'option' => 1],
                ['id' => 2, 'option' => 2],
                ['id' => 3, 'option' => 3]
            ];
        } else if ($question->rank === 4){
            $question->opciones = [
                ['id' => 1, 'option' => 1],
                ['id' => 2, 'option' => 2],
                ['id' => 3, 'option' => 3],
                ['id' => 4, 'option' => 4]
            ];
        } else if ($question->rank === 5){
            $question->opciones = [
                ['id' => 1, 'option' => 1],
                ['id' => 2, 'option' => 2],
                ['id' => 3, 'option' => 3],
                ['id' => 4, 'option' => 4],
                ['id' => 5, 'option' => 5]
            ];
        } else {
            $question->opciones = [
                ['id' => 1, 'option' => 1],
                ['id' => 2, 'option' => 2],
                ['id' => 3, 'option' => 3],
                ['id' => 4, 'option' => 4],
                ['id' => 5, 'option' => 5],
                ['id' => 6, 'option' => 6]
            ];
        }

        foreach ($question['opciones'] as $opt) {
            $count = 0;
            foreach ($question['answer'] as $answer) {
                if ($answer->survey_question_id === $question->id && (int)$answer->answer === $opt['option']) {
                    $count++;
                }

                $options[$opt['id']] = [
                    'title' => $opt['option'],
                    'vote_count' => $count
                ];
            }
        }
    }

    //yes or no
    if ($question->response_type_id === 4) {
        $question->opciones = [
            ['id' => 1, 'option' => 'No'],
            ['id' => 2, 'option' => 'Si']
        ];

        foreach ($question['opciones'] as $opt) {
            $count = 0;
            foreach ($question['answer'] as $answer) {
                if ($answer->survey_question_id === $question->id && $answer->answer === $opt['option']) {
                    $count++;
                }

                $options[$opt['id']] = [
                    'title' => $opt['option'],
                    'vote_count' => $count
                ];
            }
        }
    }

    $result[] = [
        'id' => $question->id,
        'type' => $question->response_type_id,
        'rank' => $question->rank,
        'title' => $question->question,
        'options' => $options
    ];
}
我真的不知道我将在哪里或如何计算总数,任何帮助将不胜感激

可以使用递归数组迭代器“深入”搜索数组

有关灵感,请参见下面的代码:

$iterator=new\RecursiveArrayIterator($array);
$recursive=new\recursiveIterator(
$iterator,
\递归迭代器::SELF_优先
);
$sum=0;
foreach($key=>$value的递归形式){
如果($key==='vote\u count'){
$sum+=$value;
}
}

您是否将其存储在mysql这样的数据库中?@elijah7是的,但现在它是本地的