2017-06-13 59 views
1

我有什麼是這個使用array_filter另一個array_filter後排除字(S)

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

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

); 


$i++; } 

$conditions = array("Fine", "Very Good","Good", "New"); 

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


usort($resulted, function($a, $b) { 
if ($a['listingPrice'] < $b['listingPrice']) return -1; 
if ($a['listingPrice'] > $b['listingPrice']) return 1; 
    return 0; 
}); 

$finalresult = reset($resulted); 

$ finalresult返回用最低的價格數組只包括$條件。但我需要usort部分之前增加一個額外的過濾器.. 過濾器將

$international="nternational" (like one would with a query like %nternational%) 

所以它不會是一個濾鏡陣列(但它可能是,如果它不得不),它會用排序結果$ [ 'vendorDescription'],但如果需要或更容易,它可以排序之前陣列過濾器---

只是要清楚我想排除在vendorDesription中具有nternational的任何結果。

+0

您可以用'preg_grep() 'http://php.net/manual/en/function.preg-grep.php? – Kisaragi

回答

1

如果你想

排除有nternationalvendorDescription

然後,你可以修改array_filter回調的任何結果:

$resulted = array_filter($abeResult, function($book) use ($conditions) { 
    return in_array($book['itemCondition'], $conditions) 
     && strpos($book['vendorDescription'], 'nternational') === false; 
}); 
+0

作品完美,謝謝 – ForgivenIT