Php Laravel-检查数组中的comepare值,并检查价格是否更高
我的刀片中有一些输入字段,如下所示:Php Laravel-检查数组中的comepare值,并检查价格是否更高,php,arrays,laravel,Php,Arrays,Laravel,我的刀片中有一些输入字段,如下所示: <input type="text" name="product[0][name]"> <input type="text" name="product[0][price]"> <input type="text" name="product[1][name]"> <input type="text" name="product[1][price]"> API PRODUCT ARRAY array:80
<input type="text" name="product[0][name]">
<input type="text" name="product[0][price]">
<input type="text" name="product[1][name]">
<input type="text" name="product[1][price]">
API PRODUCT ARRAY
array:80 [▼
0 => array:18 [▼
"name" => "john doe for sale"
"price" => 120
...
]
1 => array:18 [▼
"name" => "house"
"price" => 12000
..
]
3 => array:18 [▼
"name" => "unicorn"
"price" => 5100
...
]
4 => array:18 [▼
"name" => "blabla"
"price" => 60000
...
]
现在我有了另一个数组,看起来像这样:
<input type="text" name="product[0][name]">
<input type="text" name="product[0][price]">
<input type="text" name="product[1][name]">
<input type="text" name="product[1][price]">
API PRODUCT ARRAY
array:80 [▼
0 => array:18 [▼
"name" => "john doe for sale"
"price" => 120
...
]
1 => array:18 [▼
"name" => "house"
"price" => 12000
..
]
3 => array:18 [▼
"name" => "unicorn"
"price" => 5100
...
]
4 => array:18 [▼
"name" => "blabla"
"price" => 60000
...
]
现在我的问题来了
我想检查“我的产品阵列”中是否有与“API产品阵列”中相同名称的产品
如果是这种情况,那么应该检查“API产品阵列”中的一种产品的价格是否高于“我的产品阵列”
如果发生这种情况,则应将此产品添加到UserNotificationProducts数组中
我编写的代码看起来像这样,但根本不适用于我。
我需要一些帮助
foreach ($apiproducts as $key => $product)
{
if (in_array($product["name"], $myproducts["name"]))
{
if($product["price"] > (the current product of "myproducts")
array_push($productnotification, $product["name"]);
}
}
“我的产品阵列”最多有三种产品。API产品阵列最多可达100个
谢谢
我尝试了两种答案,并计算了两种解决方案的时间成本 Istiaque Ahmed的答案需要0.15902519226074 ms Hamelraj的答案(我尝试了你在发布答案之前所做的一切:D)需要0.031232833862305毫秒 因此,哈默拉的答案是更快的解决方案。谢谢你们两位 您编写了
if(在数组($product[“name”],$myproducts[“name”])
。但是,$myproducts
是一个数组数组。所以你没有从那里得到任何名字。以下代码可能会有所帮助:
foreach ($apiproducts as $key => $product)
{
$product_name=$product["name"];
for($i=0; $i<count($myproducts); $i++){
if (strcasecmp($product_name,$myproducts[$i]['name']==0) ){
if($product["price"] > (the current product of "myproducts")
array_push($productnotification, $product["name"]);
}
}// end of for loop
}
foreach($apiproducts作为$key=>$product)
{
$product_name=$product[“name”];
对于($i=0;$i(“myproducts”的当前产品)
array_push($productnotification,$product[“name”]);
}
}//循环结束
}
您编写了if(在数组($product[“name]”,$myproducts[“name]”)中)
。但是$myproducts
是一个数组。因此您无法从中获得任何名称。以下代码可能会有所帮助:
foreach ($apiproducts as $key => $product)
{
$product_name=$product["name"];
for($i=0; $i<count($myproducts); $i++){
if (strcasecmp($product_name,$myproducts[$i]['name']==0) ){
if($product["price"] > (the current product of "myproducts")
array_push($productnotification, $product["name"]);
}
}// end of for loop
}
foreach($apiproducts作为$key=>$product)
{
$product_name=$product[“name”];
对于($i=0;$i(“myproducts”的当前产品)
array_push($productnotification,$product[“name”]);
}
}//循环结束
}
您找到了解决方案还是需要其他解决方案?您找到了解决方案还是需要其他解决方案?