2017-06-05 75 views
1

裏面我相信這個問題已經被問,但我不能找到它所以這裏去...返回整個陣列的一個值的最低值輔助陣列

我有這樣的代碼(必須使用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是好,非常好,做工精細,或新的,並返回該項目離這個整個數組它將返回

({"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

創造條件,你會接受

$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); 
+0

這給了我假的結果的輸出,如果我嘗試輸出結果我得到空括號 – ForgivenIT

+0

@ForgivenIT噢,可能是因爲我不小心使用的對象符號,而不是陣列。我更新了答案。 –

+0

我一直在摸索着如何用對象符號工作,以爲我錯過了一些感謝更新,爲我省下了一大筆頭痛 – ForgivenIT