返回整个数组,以获取辅助数组中一个值的最小值

时间:2017-06-05 17:42:30

标签: php arrays min

我确信这个问题已被提出但我无法找到,所以这里......

我有这个代码(必须使用php 5.3)

foreach ($array["Book"] as $abeBooks) {

$abeResult[$i] = array(
            'itemCondition' => $abeBooks['itemCondition'],
            'isbn13' =>$abeBooks['isbn13'],
            'listingPrice' =>$abeBooks['listingPrice'],
            'Link' =>$abeBooks['listingUrl'],
            'sellerRating'=>$abeBooks['sellerRating'],

);

 $isbn13[$i] = $abeBooks["isbn13"];
 $itemCondition[$i] = $abeBooks["itemCondition"];
 $sellerRating[$i] =$abeBooks['sellerRating'];
 $Price[$i] = $abeBooks["listingPrice"];
 $Link[$i] =$abeBooks['listingUrl'];

$i++; }

返回:

[{"itemCondition":"Fair","isbn13":"9780134167398","listingPrice":"123.5","Link":"www.abebooks.com\/servlet\/BookDetailsPL?bi=22403600014&cm_ven=sws&cm_cat=sws&cm_pla=sws&cm_ite=22403600014","sellerRating":"4"},{"itemCondition":"Very Good","isbn13":"9780134167398","listingPrice":"140.22","Link":"www.abebooks.com\/servlet\/BookDetailsPL?bi=22334428082&cm_ven=sws&cm_cat=sws&cm_pla=sws&cm_ite=22334428082","sellerRating":"4"},{"itemCondition":null,"isbn13":"9780134167398","listingPrice":"480.7","Link":"www.abebooks.com\/servlet\/BookDetailsPL?bi=22173609508&cm_ven=sws&cm_cat=sws&cm_pla=sws&cm_ite=22173609508","sellerRating":"4"}]

我想要做的是找到最低的listingPrice,如果itemCondition是好的,非常好的,罚款的,或者是新的并返回该项目的整个数组,为此它将返回 < / p>

({"itemCondition":"Fair","isbn13":"9780134167398","listingPrice":"123.5","Link":"www.abebooks.com\/servlet\/BookDetailsPL?bi=22403600014&cm_ven=sws&cm_cat=sws&cm_pla=sws&cm_ite=22403600014","sellerRating":"4"})

1 个答案:

答案 0 :(得分:1)

确定您接受的条件

$conditions = array('Fair', 'Very Good', 'Fair', 'New');

过滤数组以仅包含具有这些条件的项目

$results = array_filter($abeResult, function($book) use ($conditions) {
    return in_array($book['itemCondition'], $conditions);
});

通过listingPrice

按升序对结果数组进行排序
usort($results, function($a, $b) {
    if ($a['listingPrice'] < $b['listingPrice']) return -1;
    if ($a['listingPrice'] > $b['listingPrice']) return 1;
    return 0;
});

最低价格将是您的第一个结果。

$result = reset($results);