使用array_filter在另一个array_filter之后排除一个单词

时间:2017-06-13 16:12:22

标签: php arrays sorting array-filter

我拥有的是这个

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返回一个价格最低的数组,仅包括$ conditions。但我需要在usort部分之前添加一个额外的过滤器.. 过滤器将是

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

因此它不会是一个数组过滤器(但它可能是必须的)它将使用$ results [&#39; vendorDescription&#39;]进行排序但是如果需要或更容易,它可以在数组过滤器之前排序---

要明确我想要排除任何在vendorDesription中有nternational的结果。

1 个答案:

答案 0 :(得分:1)

如果你想

  

排除nternational

vendorDescription的所有结果

然后您可以将array_filter回调修改为:

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