Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Laravel中循环使用相同键的两个数组_Php_Laravel - Fatal编程技术网

Php 如何在Laravel中循环使用相同键的两个数组

Php 如何在Laravel中循环使用相同键的两个数组,php,laravel,Php,Laravel,我发布了以下数组,其中每个复选框/复选框标记都与一个键链接(即,每个类别复选框都与一个“全球”复选框链接): 上面的foreach循环的语法是什么 注意:请注意,通过键链接在一起的数组可能大小不同,因为可以选择类别,而不选择“worldwide”(即array_combine()需要大小相同的数组)。请尝试以下代码 一个可能的解决方案可能是“分块”这些输入。可以对表单输入使用多维数组 尝试按如下方式重命名它们: <input type="checkbox" name="set[1][ca

我发布了以下数组,其中每个复选框/复选框标记都与一个键链接(即,每个类别复选框都与一个“全球”复选框链接):

上面的foreach循环的语法是什么

注意:请注意,通过键链接在一起的数组可能大小不同,因为可以选择类别,而不选择“worldwide”(即array_combine()需要大小相同的数组)。

请尝试以下代码


一个可能的解决方案可能是“分块”这些输入。可以对表单输入使用多维数组

尝试按如下方式重命名它们:

<input type="checkbox" name="set[1][catpref]" value=10> Politics
<input type="checkbox" name="set[1][worldwide]" value=1> Worldwide

<input type="checkbox" name="set[2][catpref]" value=20> Sports
<input type="checkbox" name="set[2][worldwide]" value=1> Worldwide
foreach ($request['set'] as ['catpref' => $catpref, 'worldwide' => $worldwide]) {
    var_dump($catpref, $worldwide);
}

您可以检查是否设置了worldwide键,然后将其值other wise set添加到默认值

foreach($catprefs as $key => $value){

  $save_catp = new Cpref();
  $save_catp->user_id = $user_id;
  $save_catp->qatype = $value;
  $save_catp->wwide= (array_key_exists($key, $worldwide) ? $worldwide[$key] : 'default value');
  $save_catp->type = 1;
  $save_catp->save();
}

我收到错误:未定义的偏移量:此行3:$save_catp->wwide=$worldwide[$key];我尝试过,但没有给出错误,但是现在所有类别都将wwide设置为1,即使是那些没有选中“全球范围”复选框的类别也是如此。$save_catp->wwide=isset($worldwide[$key])$全球[$key]:1;使用默认值更改“1”,它将起作用。我收到错误:为foreach()提供的参数无效。请不要盲目复制粘贴此代码。是否更改了复选框名称属性?
$request
变量中有什么?请在循环前显示
var\u dump($request)
<input type="checkbox" name="set[1][catpref]" value=10> Politics
<input type="checkbox" name="set[1][worldwide]" value=1> Worldwide

<input type="checkbox" name="set[2][catpref]" value=20> Sports
<input type="checkbox" name="set[2][worldwide]" value=1> Worldwide
foreach ($request['set'] as ['catpref' => $catpref, 'worldwide' => $worldwide]) {
    var_dump($catpref, $worldwide);
}
foreach($catprefs as $key => $value){

  $save_catp = new Cpref();
  $save_catp->user_id = $user_id;
  $save_catp->qatype = $value;
  $save_catp->wwide= (array_key_exists($key, $worldwide) ? $worldwide[$key] : 'default value');
  $save_catp->type = 1;
  $save_catp->save();
}