Php 二级字符串索引关联数组的最小值
我有这个关联数组,我想从这些数组中得到Php 二级字符串索引关联数组的最小值,php,arrays,Php,Arrays,我有这个关联数组,我想从这些数组中得到price的最低值 array (size=3) 0 => array (size=21) 'quantity' => int 6 'product_id' => int 3 'category_id' => string '2' (length=1) 'price' => float 18.73 1 => array (size=21)
price
的最低值
array (size=3)
0 =>
array (size=21)
'quantity' => int 6
'product_id' => int 3
'category_id' => string '2' (length=1)
'price' => float 18.73
1 =>
array (size=21)
'quantity' => int 21
'product_id' => int 6
'category_id' => string '1' (length=1)
'price' => float 0.26
2=>
array (size=21)
'quantity' => int 34
'product_id' => int 6
'category_id' => string '1' (length=1)
'price' => float 0.63
我试过了
foreach ($products as $key_id => $prod) {
$lowest = $prod['price'];
if($prod['price'] < $lowest) {
// what to do here
}
}
这就是reduce操作非常合适的地方:
$lowest = array_reduce($products, function ($lowest, array $product) {
return !$lowest || $product['price'] < $lowest ? $product['price'] : $lowest;
});
$lowest=array\u reduce($products,function($lowest,array$product){
return!$lost | |$product['price']<$lost?$product['price']:$lost;
});
这就是reduce操作非常合适的地方:
$lowest = array_reduce($products, function ($lowest, array $product) {
return !$lowest || $product['price'] < $lowest ? $product['price'] : $lowest;
});
$lowest=array\u reduce($products,function($lowest,array$product){
return!$lost | |$product['price']<$lost?$product['price']:$lost;
});
只有当最低价格低于当前最低价格时,才应设置最低价格
// Set $low to the max value
$low = PHP_INT_MAX;
foreach ($products as $key_id => $prod) {
// If the price is lower than the current lowest, change the lowest price
if($prod['price'] < $lowest) {
$low = $prod['price'];
}
}
//将$low设置为最大值
$low=PHP\u INT\u MAX;
foreach($key\u id=>$prod形式的产品){
//如果价格低于当前最低价格,请更改最低价格
如果($prod['price']<$lowest){
$low=$prod[‘价格’];
}
}
只有当最低价格低于当前最低价格时,才应设置最低价格
// Set $low to the max value
$low = PHP_INT_MAX;
foreach ($products as $key_id => $prod) {
// If the price is lower than the current lowest, change the lowest price
if($prod['price'] < $lowest) {
$low = $prod['price'];
}
}
//将$low设置为最大值
$low=PHP\u INT\u MAX;
foreach($key\u id=>$prod形式的产品){
//如果价格低于当前最低价格,请更改最低价格
如果($prod['price']<$lowest){
$low=$prod[‘价格’];
}
}
在php中>=5.5
$min = min(array_column($products, 'price'));
php>=5.3(由deceze建议)
在php中>=5.0
$prices = array();
foreach ($products as $product) {
$prices[] = $product['price'];
}
$min = min($prices);
编辑
要查找产品id和用户id,可以使用以下方法:
$min = PHP_INT_MAX;
$product_id = 0;
foreach ($products as $product) {
if ($product['price'] < $min) {
$product_id = $product['product_id'];
$min = $product['price'];
}
}
$min=PHP\u INT\u MAX;
$product_id=0;
foreach($products as$product){
如果($product['price']<$min){
$product_id=$product['product_id'];
$min=$product['price'];
}
}
在php中>=5.5
$min = min(array_column($products, 'price'));
php>=5.3(由deceze建议)
在php中>=5.0
$prices = array();
foreach ($products as $product) {
$prices[] = $product['price'];
}
$min = min($prices);
编辑
要查找产品id和用户id,可以使用以下方法:
$min = PHP_INT_MAX;
$product_id = 0;
foreach ($products as $product) {
if ($product['price'] < $min) {
$product_id = $product['product_id'];
$min = $product['price'];
}
}
$min=PHP\u INT\u MAX;
$product_id=0;
foreach($products as$product){
如果($product['price']<$min){
$product_id=$product['product_id'];
$min=$product['price'];
}
}
首先尝试查找正则数组的最小值(仅数组中填充了数字)。然后修改就很容易了。首先,试着找到正则数组的最小值(只需要填充数字的数组)。然后修改就很容易了。另外:
min(数组映射(函数(数组$product){return$product['price'];},$products))
@deceze添加了您的建议这很有效。。。。如果我想获取该阵列的产品id呢???例如,我的问题中的数组。。。我想以product_id=>6,price=>0.26的形式返回数组。另外:min(数组映射(函数(数组$product){return$product['price'];},$products))
@deceze添加了您的建议这很有效。。。。如果我想获取该阵列的产品id呢???例如,我的问题中的数组。。。我想以product\u id=>6,price=>0.26的形式返回数组,如果我想获取该数组的product\u id呢???例如,我的问题中的数组。。。我想返回数组作为product\u id=>6,price=>0。26@user返回$最低| |$产品['price']<$最低['price']$product:$lower
如果我想获取该数组的产品id该怎么办???例如,我的问题中的数组。。。我想返回数组作为product\u id=>6,price=>0。26@user返回$最低| |$产品['price']<$最低['price']$产品:$最低