二级索引的字符串关联数组最低值

编程入门 行业动态 更新时间:2024-10-24 01:58:56
本文介绍了二级索引的字符串关联数组最低值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这样的关联数组,我想获得价格的那些阵列的最低值

阵列(大小= 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

更多推荐

二级索引的字符串关联数组最低值

本文发布于:2023-11-22 20:06:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618827.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串   索引   低值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!