【问题标题】:Array_merge wont mergeArray_merge 不会合并
【发布时间】:2018-08-17 01:03:21
【问题描述】:

有一个朋友让我对这个网站进行一些更改。它使用 Laravel 框架。

我已经进行了一些数据库调用来获取一些数据,但是当我尝试将数据合并到另一个数组中时,什么也没有出现。这里似乎有什么问题?

public function show_opfolgningsoversigt() {
    $complete = array();

    $surveys = DB::table('surveys')->where('company_id', \Auth::user()->company_id)->get();
    foreach($surveys as $survey) {
        $questions = DB::table('questions')->where('survey_id', $survey->id)->get();
        foreach($questions as $question) {
            $answers = DB::table('answers')->where('question_id', $question->id)->get();
            foreach($answers as $answer) {
                array_merge($complete, array("recipient" => $answers->recipient_id, "followup" => $answers->followup);
            }
        }
    }
    $followups = $complete;

    return view('survey.opfolgningsoversigt')->with('followups', $followups);
}

除了 $complete 之外的所有变量都输出它应该输出的内容。这意味着 $answers->recipient_id 和 $answers->followup 返回 int。

【问题讨论】:

  • "这似乎是什么问题?" -- 问题是你希望我们猜测输入是什么,预期输出和实际输出代码。在假设 array_merge() 不起作用之前,请确保代码从数据库中获取一些数据。

标签: php arrays laravel merge


【解决方案1】:

array_merge 返回一个数组,在您的代码中您不使用返回的结果。

$complete = array_merge($complete, array("recipient" => $answers->recipient_id, "followup" => $answers->followup);

可能是你想要达到的目标。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-06
    • 2018-06-30
    相关资源
    最近更新 更多