php stdClass对象嵌套的子值对象

时间:2019-01-29 17:08:13

标签: php laravel object stdclass

我在laravel中处理stdClass对象。我必须合并两个表中的值。即第一个包含一些问题,而另一个包含他们的多项选择答案。如何以问题对象可以包含其答案对象作为嵌套对象的方式组合结果。我想要这样的结构:

stdClass Object
(
[question_id] => 4
[title] => How to add data?
[options] => Array
(
[0] => stdClass Object
(
[id] => 1
[question_id] => 4
[option_title] => By any device
)

[1] => stdClass Object
(
[id] => 2
[question_id] => 4
[option_title] => By mobile
)

[2] => stdClass Object
(
[id] => 3
[question_id] => 4
[option_title] => By Laptop
)
)
stdClass Object
(
[question_id] => 5
[title] => Where to add data?
[options] => Array
(
[0] => stdClass Object
(
[id] => 1
[question_id] => 5
[option_title] => Lahore
)

[1] => stdClass Object
(
[id] => 2
[question_id] => 5
[option_title] => Karachi
)

[2] => stdClass Object
(
[id] => 3
[question_id] => 5
[option_title] => Multan
)

);

1 个答案:

答案 0 :(得分:0)

使用带键的foreach有点前卫。最终开发了以下代码,并且运行良好:

foreach ($question_data as $key => $question) {
            foreach ($option_data as $key2 => $opt) {
                if ($question_data[$key]->question_id == $option_data[$key2]['question_id'] && $question->q_type != 3) {
                    $question->options[] = [
                        'question_id' => $option_data[$key2]['question_id'],
                        'option_id' => $option_data[$key2]['id'],
                        'option_title' => $option_data[$key2]['option_title']
                    ];
                } else if($question->q_type == 3) {
                    $question->options = [];
                }
            }
        }

它提供了以下结果:

"question_data": [
        {
            "question_id": 2,
            "title": "Describe about visit",
            "q_type": 3,
            "question_group_id": 1,
            "options": []
        },
        {
            "question_id": 3,
            "title": "How to upload data",
            "q_type": 2,
            "question_group_id": 1,
            "options": [
                {
                    "question_id": 3,
                    "option_id": 4,
                    "option_title": "By any device"
                },
                {
                    "question_id": 3,
                    "option_id": 5,
                    "option_title": "By mobile"
                },
                {
                    "question_id": 3,
                    "option_id": 6,
                    "option_title": "By camera"
                },
                {
                    "question_id": 3,
                    "option_id": 7,
                    "option_title": "By Laptop"
                },
                {
                    "question_id": 3,
                    "option_id": 8,
                    "option_title": "No answer"
                }
            ]
        },
        {
            "question_id": 4,
            "title": "How to add question?",
            "q_type": 3,
            "question_group_id": 1,
            "options": []
        },
        {
            "question_id": 5,
            "title": "Multi selection is working?",
            "q_type": 1,
            "question_group_id": 1,
            "options": [
                {
                    "question_id": 5,
                    "option_id": 9,
                    "option_title": "Yes"
                },
                {
                    "question_id": 5,
                    "option_id": 10,
                    "option_title": "No"
                },
                {
                    "question_id": 5,
                    "option_id": 11,
                    "option_title": "Not Confirmed"
                },
                {
                    "question_id": 5,
                    "option_id": 12,
                    "option_title": "In progress"
                },
                {
                    "question_id": 5,
                    "option_id": 13,
                    "option_title": "Testing"
                }
            ]
        }
    ]