为什么答案未正确存储在答案表中? (数组问题)

时间:2018-08-20 11:11:31

标签: php arrays

如果用户正在会议中进行注册,并且注册表单会收集以下数据:

id  |    participant_id    |    question_id    |    answer
159 |       153            |         1         |    answer1p1
161 |       153            |         2         |    answer2p1
163 |       154            |         1         |    answer1p2
165 |       154            |         2         |    answer2p2  

然后需要将参与者信息存储在参与者表中,并将与每个参与者关联的答案存储在答案表中。插入参与者表工作正常。

问题:要插入答案表中无法正常工作。在数据库中应将其存储为:

id  |    participant_id    |    question_id    |    answer
159 |       153            |         1         |     answer1p1
161 |       153            |         1         |    answer1p2
163 |       154            |          1        |    answer1p1
165 |       154            |          1        |    answer1p2  
160 |       153            |          2        |    answer2p1
162 |       153            |          2        |    answer2p2
164 |       154            |          2        |    answer2p1
166 |       154            |          2        |    answer2p2

但是这样存储:

foreach ($request->all()['name'] as $key => $nameArray) {
    foreach ($nameArray as $nameKey => $name) {
        $participant_result = Participant::create([
            'name' => $name,
            'surname' => $request['surname'][$key][$nameKey],
            'registration_id' => $registration->id,
            'registration_type_id' => $key
        ]);

        if (isset($request['answer'][$key])) {
            foreach ($request->all()['answer'][$key] as $rID => $answers) {
                foreach ($answers as $question_id => $answer) {
                    $answer = Answer::create([
                        'question_id' => $question_id,
                        'participant_id' => $participant_result->id,
                        'answer' => $answer,
                    ]);
                }
            }
        }
    }
}

要插入参与者和答案表的代码:

#include <chrono>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <immintrin.h>
#include <random>

using f64 = double;
using s64 = int64_t;
using u64 = uint64_t;

static constexpr u64 cycles = 24;
static constexpr u64 sample_max = 1000000;

f64 sse_sqrt(const f64 x) {
    __m128d root = _mm_sqrt_pd(_mm_load_pd(&x));
    return *(reinterpret_cast<f64*>(&root));
}

constexpr f64 carmack_sqrt(const f64 x) {
    union {
        f64 x;
        s64 i;
    } u = {};
    u.x = x;
    u.i = 0x5fe6eb50c7b537a9 - (u.i >> 1);
    f64 xhalf = 0.5 * x;
    u.x = u.x * (1.5 - xhalf * u.x * u.x);
    # u.x = u.x * (1.5 - xhalf * u.x * u.x);
    # u.x = u.x * (1.5 - xhalf * u.x * u.x);
    # ... so on, if you want more precise result ...
    return u.x * x;
}

int main(int /* argc */, char ** /*argv*/) {
    std::random_device r;
    std::default_random_engine e(r());
    std::uniform_real_distribution<f64> dist(1, sample_max);
    std::deque<f64> samples(sample_max);
    for (auto& sample : samples) {
        sample = dist(e);
    }

    // std sqrt
    {
        std::cout << "> Measuring std sqrt.\r\n> Please wait . . .\r\n";
        f64 result = 0;
        auto t1 = std::chrono::high_resolution_clock::now();
        for (auto cycle = 0; cycle < cycles; ++cycle) {
            for (auto& sample : samples) {
                result += std::sqrt(static_cast<f64>(sample));
            }
        }
        auto t2 = std::chrono::high_resolution_clock::now();
        auto dt = t2 - t1;
        std::cout << "> Accumulated result: " << std::setprecision(19) << result << "\n";
        std::cout << "> Total execution time: " <<
        std::chrono::duration_cast<std::chrono::milliseconds>(dt).count() << " ms.\r\n\r\n";
    }

    // sse sqrt
    {
        std::cout << "> Measuring sse sqrt.\r\n> Please wait . . .\r\n";
        f64 result = 0;
        auto t1 = std::chrono::high_resolution_clock::now();
        for (auto cycle = 0; cycle < cycles; ++cycle) {
            for (auto& sample : samples) {
                result += sse_sqrt(static_cast<f64>(sample));
            }
        }
        auto t2 = std::chrono::high_resolution_clock::now();
        auto dt = t2 - t1;
        std::cout << "> Accumulated result: " << std::setprecision(19) << result << "\n";
        std::cout << "> Total execution time: " <<
        std::chrono::duration_cast<std::chrono::milliseconds>(dt).count() << " ms.\r\n\r\n";
    }

    // carmack sqrt
    {
        std::cout << "> Measuring carmack sqrt.\r\n> Please wait . . .\r\n";
        f64 result = 0;
        auto t1 = std::chrono::high_resolution_clock::now();
        for (auto cycle = 0; cycle < cycles; ++cycle) {
            for (auto& sample : samples) {
                result += carmack_sqrt(static_cast<f64>(sample));
           }
        }
        auto t2 = std::chrono::high_resolution_clock::now();
        auto dt = t2 - t1;
        std::cout << "> Accumulated result: " << std::setprecision(19) << result << "\n";
        std::cout << "> Total execution time: " <<
        std::chrono::duration_cast<std::chrono::milliseconds>(dt).count() << " ms.\r\n\r\n";
    }

    std::cout << "> Press any key to exit . . .\r\n";
    std::getchar();
    return 0;
}

1 个答案:

答案 0 :(得分:2)

您始终会为每个参与者添加所有答案-而您应该仅添加他自己的答案。 您可能可以尝试这样的事情:

foreach ($request['name'] as $reg_type => $nameArray) 
{
    foreach ($nameArray as $nameKey => $name) 
    {
        $participant_result = Participant::create([
            'name' => $name,
            'surname' => $request['surname'][$reg_type][$nameKey],
            'registration_id' => $registration->id,
            'registration_type_id' => $reg_type
        ]);

        if (isset($request['answer'][$reg_type][$nameKey])) 
        {
            foreach ($request['answer'][$reg_type][$nameKey] as $question_id => $answer) 
            {
                $answer = Answer::create([
                    'question_id' => $question_id,
                    'participant_id' => $participant_result->id,
                    'answer' => $answer,
                ]);
            }
        }
    }
}