if语句中的第一个foreach语句没有启动,直接跳到第二个?

时间:2018-03-25 21:58:28

标签: php arrays laravel foreach

所以我遇到了问题。我有两个if语句请求 - >来自表格的数据。但是,当我同时检查时,它会跳过第一个并转到第二个。第一个中的foreach语句甚至没有启动,我也不知道为什么。

这是我的代码:

if ($request->location_verification == 'on') {
    $get_data = [];
    foreach (array_chunk($results, 50) as $chunk_result) {
        foreach ($chunk_result as $r) {

            $client_name = trim($r['first_name'] . ' ' . $r['last_name']);
            if ($client_name == '') {
                $check_name = ContactList::where('phone_number', $r['phone_number'])->first();
                if ($check_name) {
                    $client_name = trim($check_name->first_name . ' ' . $check_name->last_name);
                    if ($client_name == '') {
                        $client_name = 'no name';
                    }
                } else {
                    $client_name = 'no name';
                }
            }
            $client_name = urlencode($client_name);

            $url = url('user/get-location/' . Auth::guard('client')->user()->id . '/' . $r['phone_number'] . '/' . $client_name);
            $get_message = $message . "\n" . $url;
            $r = array_merge($r, ['message' => $get_message]);
            array_push($get_data, $r);
        }
    }
} else {
    $get_data = $results;
}


if ($request->attachment_tracking == 'on') {
    $get_data = [];
    $lastMessage = MessageFiles::orderBy('message_id', 'desc')->first();
    if ($files = $request->file('files')) {
        if ($first == true) {
            if (count($lastMessage) >= 1) {
                $messageCount = $lastMessage->message_id + 1;
            } else {
                $messageCount = 1;
            }
            foreach ($files as $f) {
                $destinationPath = public_path() . '/assets/message_files/';
                $file_name = $f->getClientOriginalName();
                $extension = $f->getClientOriginalExtension();
// Available alpha caracters
                $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

// generate a pin based on 2 * 7 digits + a random character
                $pin = mt_rand(1000000, 9999999)
                    . mt_rand(1000000, 9999999)
                    . $characters[rand(0, strlen($characters) - 1)];

// shuffle the result
                $string = str_shuffle($pin);
                $f->move($destinationPath, $string . '.' . $extension);


                $tf = new MessageFiles();
                $tf->message_id = $messageCount;
                $tf->cl_id = Auth::guard('client')->user()->id;
                $tf->message = $get_message;
                $tf->file = $string . '.' . $extension;
                $tf->save();
                $first = false;
            }
        }
        foreach (array_chunk($results, 50) as $chunk_result) {
            foreach ($chunk_result as $r) {

                $client_name = trim($r['first_name'] . ' ' . $r['last_name']);
                if ($client_name == '') {
                    $check_name = ContactList::where('phone_number', $r['phone_number'])->first();
                    if ($check_name) {
                        $client_name = trim($check_name->first_name . ' ' . $check_name->last_name);
                        if ($client_name == '') {
                            $client_name = 'no name';
                        }
                    } else {
                        $client_name = 'no name';
                    }
                }
                $client_name = urlencode($client_name);

                $url = url('user/get-attachment/' . Auth::guard('client')->user()->id . '/' . $r['phone_number'] . '/' . $client_name);
                $get_message = $message . "\n" . $url;
                $r = array_merge($r, ['message' => $get_message]);
                array_push($get_data, $r);
            }
        }
    } else {
        return redirect('user/sms/add-recurring-sms')->withInput(Input::all())->with([
            'message' => "No uploaded files found. Please upload a file to use attachment tracking.",
            'message_important' => true
        ]);
    }
}

基本上,location_verification正在运行,但foreach语句不是。 然后,当attachment_verification运行时,foreach (array_chunk运行完美,但不会location_verification

我尝试切换它们,第一个foreach(array_chunk永远不会运行。可能是因为价值相似吗?

我迷失了。

编辑:array_push没有发生,并且没有为第一个if语句更新新消息。

0 个答案:

没有答案