dtntjwkl83750 2017-06-05 17:42
浏览 72
已采纳

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

I am sure this question has been asked but i cant find it so here goes...

I have this code (have to use 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++; }

it returns :

[{"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"}]

what i would like to do is find if the lowest listingPrice if the itemCondition is good,very good, fine, or new and return the entire array for that item ex for this it would return

({"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条回答 默认 最新

  • duanpie2834 2017-06-05 19:10
    关注

    Establish the conditions you'll accept

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

    Filter the array to only include items with those conditions

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

    Sort the resulting array in ascending order by listingPrice

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

    The lowest price will be your first result.

    $result = reset($results);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?