二级索引的字符串关联数组最低值 [英] Lowest value of two level String indexed Associative Array
本文介绍了二级索引的字符串关联数组最低值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的关联数组,我想获得价格
的那些阵列的最低值
阵列(大小= 3)
0 =>
阵列(大小= 21)
量= GT; INT 6
'的product_id => INT 3
CATEGORY_ID'=>串'2'(长度= 1)
'价格'=>浮18.73
1 =>
阵列(大小= 21)
量= GT;诠释21
'的product_id => INT 6
CATEGORY_ID'=>串1(长度= 1)
'价格'=>浮动0.26
2 =>
阵列(大小= 21)
量= GT;诠释34
'的product_id => INT 6
CATEGORY_ID'=>串1(长度= 1)
'价格'=>浮动0.63
我曾尝试
的foreach($产品为$ KEY_ID => $ PROD){
最低$ = $督促['价格'];
如果($ PROD ['价格'< $最低){
//在这里做什么
}
}
我想以最低的价格和它的的product_id
太,像
的product_id => 6,价格= GT; 0.26
解决方案
在PHP> = 5.5
$分钟=分钟(array_column($产品,'价格'));
在PHP> = 5.3(由deceze建议)
$分钟=分钟(array_map(函数(数组$产品){$收益产品['价格'];} $产品));
在PHP> = 5.0
$价格=阵列();的foreach($产品为$产品){
$价格[] = $产品['价格'];
}$分= MIN($价格);
修改
要找到两者的product_id,你可以这样做:
$分钟= PHP_INT_MAX;
$的product_id = 0;的foreach($产品为$产品){
如果($产品['价格'< $分钟){
$的product_id = $产品['PRODUCT_ID'];
$分= $产品['价格'];
}
}
阵列列手册页结果
分钟手册页结果
array_map手册页结果
匿名函数手册页
I have this associative array, I want to get price
's lowest value from those arrays
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
I have tried
foreach ($products as $key_id => $prod) {
$lowest = $prod['price'];
if($prod['price'] < $lowest) {
// what to do here
}
}
I want to get product with lowest price and its product_id
too, something like
product_id => 6 , price => 0.26
解决方案
In php >= 5.5
$min = min(array_column($products, 'price'));
In php >= 5.3(suggested by deceze)
$min = min(array_map(function (array $product) { return $product['price']; }, $products));
In php >= 5.0
$prices = array();
foreach ($products as $product) {
$prices[] = $product['price'];
}
$min = min($prices);
EDIT
To find both product_id and you could use this:
$min = PHP_INT_MAX;
$product_id = 0;
foreach ($products as $product) {
if ($product['price'] < $min) {
$product_id = $product['product_id'];
$min = $product['price'];
}
}
array column manual page
min manual page
array_map manual page
anonymous functions manual page
这篇关于二级索引的字符串关联数组最低值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文