【问题标题】:Laravel Collection Add Values To List Of Array Filtering Specific ValueLaravel 集合将值添加到数组过滤特定值列表
【发布时间】:2021-03-16 08:16:20
【问题描述】:

我正在以这种格式从数据库接收数据

{
    "status_code": "00",
    "data": {
        "id": 1,
        "applicant_id": 1,
        "book_date_time": "2020-12-31 13:00:00",
        "status": "On Lock",
        "reference_number": "R2361-3961000001",
        "questionnaire_ids": [
            {
                "id": 1,
                "name": "Are you experiencing any of these symptoms? Please select all that apply",
                "answer": [
                    {
                        "id": 1,
                        "name": "Fever, chills, or sweating"
                    }
                ]
            },
            {
                "id": 2,
                "name": "Do any of these apply to you? Please select all that apply",
                "answer": [
                    {
                        "id": 7,
                        "name": "Moderate to serve asthma or chronic lung disease"
                    }
                ]
            },
            {
                "id": 3,
                "name": "In the last 14 days, have you travelled internationally?",
                "answer": [
                    {
                        "id": 17,
                        "name": "No"
                    },
                    {
                        "id": 18,
                        "name": "Yes"
                    }
                ]
            },
            {
                "id": 4,
                "name": "In the last 14 days, have you been in an area where COVID-19 is widespread",
                "answer": [
                    {
                        "id": 20,
                        "name": "Yes"
                    }
                ]
            },
            {
                "id": 5,
                "name": "In the last 14 days, what is your exposure to others who are known to have COVID-19?",
                "answer": [
                    {
                        "id": 22,
                        "name": "I live with someone who has COVID-19"
                    }
                ]
            },
            {
                "id": 6,
                "name": "Do you live or work in medical facility?",
                "answer": [
                    {
                        "id": 29,
                        "name": "No, I don't live or work or plan to live or work in a care facility"
                    }
                ]
            },
        ],
        "applicant_detail": {
            "id": 1,
            "user_id": 1,
            "first_name": "Mohd Farhan",
            "middle_name": "Bin",
            "last_name": "Ramli",
            "email": "farhanz95@gmail.com",
            "mobile_no": null
        },
        "transaction": null
    }
}

我需要从外部来源附加一个数据以放入问卷调查 ID,其中数组的内容的答案为 id 18,如下所示

{
    "id": 3,
    "name": "In the last 14 days, have you travelled internationally?",
    "answer": [
        {
            "id": 17,
            "name": "No"
        },
        {
            "id": 18,
            "name": "Yes",
            // Start of the data that need to be inserted
            "field_input": {
                "place_travelled_internationally": "Kuala Lumpur",
                "date_travelled_internationally": "2020-10-10"
            }
            // End of the data that need to be inserted
        }
    ]
},

这样会变成这样

{
    "status_code": "00",
    "data": {
        "id": 1,
        "applicant_id": 1,
        "book_date_time": "2020-12-31 13:00:00",
        "status": "On Lock",
        "reference_number": "R2361-3961000001",
        "questionnaire_ids": [
            {
                "id": 1,
                "name": "Are you experiencing any of these symptoms? Please select all that apply",
                "answer": [
                    {
                        "id": 1,
                        "name": "Fever, chills, or sweating"
                    }
                ]
            },
            {
                "id": 2,
                "name": "Do any of these apply to you? Please select all that apply",
                "answer": [
                    {
                        "id": 7,
                        "name": "Moderate to serve asthma or chronic lung disease"
                    }
                ]
            },
            {
                "id": 3,
                "name": "In the last 14 days, have you travelled internationally?",
                "answer": [
                    {
                        "id": 17,
                        "name": "No"
                    },
                    {
                        "id": 18,
                        "name": "Yes",
                        // Start of the data that need to be inserted
                        "field_input": {
                           "place_travelled_internationally": "Kuala Lumpur",
                           "date_travelled_internationally": "2020-10-10"
                        }
                        // End of the data that need to be inserted
                    }
                ]
            },
            {
                "id": 4,
                "name": "In the last 14 days, have you been in an area where COVID-19 is widespread",
                "answer": [
                    {
                        "id": 20,
                        "name": "Yes"
                    }
                ]
            },
            {
                "id": 5,
                "name": "In the last 14 days, what is your exposure to others who are known to have COVID-19?",
                "answer": [
                    {
                        "id": 22,
                        "name": "I live with someone who has COVID-19"
                    }
                ]
            },
            {
                "id": 6,
                "name": "Do you live or work in medical facility?",
                "answer": [
                    {
                        "id": 29,
                        "name": "No, I don't live or work or plan to live or work in a care facility"
                    }
                ]
            },
        ],
        "applicant_detail": {
            "id": 1,
            "user_id": 1,
            "first_name": "Mohd Farhan",
            "middle_name": "Bin",
            "last_name": "Ramli",
            "email": "farhanz95@gmail.com",
            "mobile_no": null
        },
        "transaction": null
    }
}

我尝试过使用下面的普通数组替换,但它不是动态的,因为它会在键更改时抛出错误

$value = '{"answer_input":{"18":{"place_travelled_internationally":"Sungai Wijaya Resources Sdn Bhd","date_travelled_internationally":"2020-12-30"}},"questions":[1,2,3,4,5,6],"answers":[1,7,18,20,22,27]}';

$questionnaires_data = json_decode($value, true);
$questions = collect($questionnaires_data['questions']);
$answers = collect($questionnaires_data['answers']);
$answer_input = collect($questionnaires_data['answer_input']);

foreach ($questions as $key => $question) {
    $questionnaires[$key] = Question::whereId($question)->select('id', 'name')->first();
    $questionnaires[$key]['answer'] = Answer::whereIn('id', $answers)->select('id', 'name')->where('question_id', $question)->get();
}

if ($answers->contains('18')) {
    $questionnaires[2]['answer'][1]['field_input'] = $answer_input[18];
}

return $questionnaires;

我尝试过使用集合地图,但我无法实现解决方案,因为我不明白如何进行这项工作,我能得到的最接近如下

代码

$value = '{"answer_input":{"18":{"place_travelled_internationally":"Sungai Wijaya Resources Sdn Bhd","date_travelled_internationally":"2020-12-30"}},"questions":[1,2,3,4,5,6],"answers":[1,7,18,20,22,27]}';


$questionnaires_data = json_decode($value, true);
$questions = collect($questionnaires_data['questions']);
$answers = collect($questionnaires_data['answers']);
$answer_input = collect($questionnaires_data['answer_input']);

foreach ($questions as $key => $question) {
    $questionnaires[$key] = Question::whereId($question)->select('id', 'name')->first();
    $questionnaires[$key]['answer'] = Answer::whereIn('id', $answers)->select('id', 'name')->where('question_id', $question)->get();
}
if ($answers->contains('18')) {
    $questionnaires = collect($questionnaires)->map(function ($row) {
        return collect($row)->map(function ($row2) {
            return $row2;
        });
    })->values()->all();
}

return $questionnaires;

输出(不需要的输出)

{
    "status_code": "00",
    "data": {
        "id": 1,
        "applicant_id": 1,
        "book_date_time": "2020-12-31 13:00:00",
        "status": "Pending Payment",
        "reference_number": "R2361-3961000001",
        "questionnaire_ids": [
            {
                "id": 1,
                "name": "Are you experiencing any of these symptoms? Please select all that apply",
                "answer": [
                    {
                        "id": 1,
                        "name": "Fever, chills, or sweating"
                    }
                ]
            },
            {
                "id": 2,
                "name": "Do any of these apply to you? Please select all that apply",
                "answer": [
                    {
                        "id": 7,
                        "name": "Moderate to serve asthma or chronic lung disease"
                    }
                ]
            },
            {
                "id": 3,
                "name": "In the last 14 days, have you travelled internationally?",
                "answer": [
                    {
                        "id": 18,
                        "name": "Yes"
                    }
                ]
            },
            {
                "id": 4,
                "name": "In the last 14 days, have you been in an area where COVID-19 is widespread",
                "answer": [
                    {
                        "id": 20,
                        "name": "Yes"
                    }
                ]
            },
            {
                "id": 5,
                "name": "In the last 14 days, what is your exposure to others who are known to have COVID-19?",
                "answer": [
                    {
                        "id": 22,
                        "name": "I live with someone who has COVID-19"
                    }
                ]
            },
            {
                "id": 6,
                "name": "Do you live or work in medical facility?",
                "answer": [
                    {
                        "id": 27,
                        "name": "I have live or worked in Hospital or other care facility in the past 14 days. This includes volunteering"
                    }
                ]
            }
        ],
        "applicant_detail": {
            "id": 1,
            "user_id": 1,
            "first_name": "Mohd Farhan",
            "middle_name": "Bin",
            "last_name": "Ramli",
            "email": "farhanz95@gmail.com",
            "mobile_no": null
        },
        "transaction": {
            "id": 1,
            "booking_id": 1,
            "status": "Pending",
            "order_no": "HDN000001",
            "remarks": "HDN Booking Payment",
            "transaction_date": "2020-12-04 17:37:56",
            "pgw_transaction_response": null
        }
    }
}

请指导如何解决此问题。

【问题讨论】:

  • 请添加您尝试过的代码
  • 嗨,我已经更新了问题并添加了我尝试过的代码
  • 更新,问题已解决!,谢谢!,我附上解决方案

标签: arrays laravel laravel-collection


【解决方案1】:

解决这个问题的方法是使用 laravel 的 map + contains + put 集合,如下所示

$questionnaires_data = json_decode($value, true);
$questions = collect($questionnaires_data['questions']);
$answers = collect($questionnaires_data['answers']);
$answer_input = collect($questionnaires_data['answer_input']);

foreach ($questions as $key => $question) {
    $questionnaires[$key] = Question::whereId($question)->select('id', 'name')->first();
    $questionnaires[$key]['answer'] = Answer::whereIn('id', $answers)->select('id', 'name')->where('question_id', $question)->get();
}

if ($answers->contains('18')) {
    $questionnaires = collect($questionnaires)->map(function ($item) use ($answer_input) {
        return collect($item)->map(function ($item2, $key2) use ($answer_input) {
            if ($key2 == 'answer') {
                return collect($item2)->map(function ($item3, $key3) use ($answer_input) {
                    if (collect($item3)->contains(18)) {
                        return collect($item3)->put('answer_input', $answer_input[18]);
                    } else {
                        return $item3;
                    }
                });
            } else {
                return $item2;
            }
        })->all();
    })->values()->all();
}

return $questionnaires;

【讨论】:

    猜你喜欢
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 2015-01-22
    • 2014-06-20
    • 2017-10-02
    • 1970-01-01
    • 2017-04-20
    相关资源
    最近更新 更多