Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.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 传递给减量法的非数值_Php_Arrays_Laravel_Foreach - Fatal编程技术网

Php 传递给减量法的非数值

Php 传递给减量法的非数值,php,arrays,laravel,foreach,Php,Arrays,Laravel,Foreach,$numArray的结果: 数组(3){[0]=>int(6)[1]=>int(8)[2]=>int(24)}数组(2){[0]=>int(1)[1]=>int(2)} 我想运行查询,但出现以下错误:传递给减量方法的非数值。为什么?在减量方法中传递要减量的值 尝试以下方法 foreach($menuItem1 as &$a){ $a = $a*$amount; } $numArray = array_map('intval', $menuItem1); foreach($menu

$numArray的结果:
数组(3){[0]=>int(6)[1]=>int(8)[2]=>int(24)}数组(2){[0]=>int(1)[1]=>int(2)}


我想运行查询,但出现以下错误:传递给减量方法的非数值。为什么?

减量方法中传递要减量的值

尝试以下方法

foreach($menuItem1 as &$a){
  $a = $a*$amount;
}

$numArray = array_map('intval', $menuItem1);

foreach($menuItem2 as $in){
    DB::table('ingredients')->where('id', $in)->decrement('ingredient_stock', $numArray);
}

减量
需要一个数值、一个数字,而不是一个数字数组

您需要重新构造
$numaray
。您可以将其设置为一个关联数组,其中菜单项id作为键
[$menuItem1ID=>valueForMenuItem1,$menuItem2ID=>valueForMenuItem2,]

然后在您的循环中,而不是
->减量('component_stock',$numaray)

减量('Component_stock',$numArray[$in])

$numArray
是数组,好吗?它实际上不是一个数值。好的,你有解决方案吗?试试dd($numArray)?没有更多的错误,但是现在查询没有执行。DB::表格(‘成分’)->其中(‘id’,$in)->减量(‘成分’库存),dd($numArray));为什么“不执行”?错误?很乐意帮忙!!
$num_i = 0;
foreach($menuItem2 as $in){
    DB::table('ingredients')->where('id', $in)->decrement('ingredient_stock', $numArray[$num_i]);
    $num_i = $num_i + 1;
}