猿问

重新排列 PHP 数组并返回最低的 3

我有一个像这样的数组列表


[0] => Array

    (

        [id] => 104

        [book_id] => 32

        [price] => 55

    )


[1] => Array

    (

        [id] => 117

        [book_id] => 76

        [price] => 65

    )


[2] => Array

    (

        [id] => 135

        [book_id] => 77

        [price] => 65

    )


[3] => Array

    (

        [id] => 100

        [book_id] => 78

        [price] => 65

    )


[4] => Array

    (

        [id] => 110

        [book_id] => 21

        [price] => 85

    )


[5] => Array

    (

        [id] => 107

        [book_id] => 35

        [price] => 90

    )


[6] => Array

    (

        [id] => 108

        [book_id] => 64

        [price] => 90

    )


[7] => Array

    (

        [id] => 130

        [book_id] => 101

        [price] => 100

    )

如果你看到这个数组,它是price从低到大排列的。我正在尝试重新排列数组以仅获得 3 个最低价格并过滤掉其余价格。意味着,当我进行过滤时,我的数组必须包含 55-85 的价格,即索引 0-4。


有没有办法像这样过滤它?谢谢你。


一只甜甜圈
浏览 142回答 3
3回答

喵喵时光机

我认为使用基本的 foreach 循环可能会相当简单。如果你看到这个数组,它是按价格从低到大排列的。所以,我假设你的价格已经排序,额外的排序是多余的。您可以循环并维护遇到的不同价格的数量。一旦$cnt达到4,您就可以停止收集数据并打印结果。<?php$cnt = 0;$result = [];foreach($arr as $index => $data){&nbsp; &nbsp; if($index === 0 || $data['price'] > $arr[$index-1]['price']){&nbsp; &nbsp; &nbsp; &nbsp; $cnt++;&nbsp; &nbsp; }&nbsp; &nbsp; if($cnt === 4) break;&nbsp; &nbsp; $result[] = $data;}&nbsp; &nbsp;&nbsp;echo "<pre>";print_r($result);

慕标琳琳

这是一种相当手动的方法,它从数据中提取价格并在过滤器中使用它...$items = array(&nbsp; array( 'id' => 104, 'book_id' => 32, 'price' => 55 ),&nbsp; array( 'id' => 117, 'book_id' => 76, 'price' => 65 ),&nbsp; array( 'id' => 135, 'book_id' => 77, 'price' => 65 ),&nbsp; array( 'id' => 100, 'book_id' => 78, 'price' => 65 ),&nbsp; array( 'id' => 101, 'book_id' => 21, 'price' => 85 ),&nbsp; array( 'id' => 107, 'book_id' => 35, 'price' => 90 ),&nbsp; array( 'id' => 108, 'book_id' => 64, 'price' => 90 ),&nbsp; array( 'id' => 130, 'book_id' => 101, 'price' => 100 ),);// extract unique prices out of the data$prices = array_unique( array_column( $items, 'price' ) );// sort the prices (ascending)sort( $prices );// extract three prices$threePrices = array_slice( $prices, 0, 3 );// filter the items that have a price in the lowest three prices array$lowestItems = array_filter( $items, function( $item ) use ( $threePrices ) {&nbsp; return in_array( $item['price'], $threePrices );});print_r( $lowestItems );//&nbsp; Array//&nbsp; (//&nbsp; &nbsp; &nbsp; [0] => Array//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [id] => 104//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [book_id] => 32//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [price] => 55//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )//&nbsp;&nbsp;//&nbsp; &nbsp; &nbsp; [1] => Array//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [id] => 117//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [book_id] => 76//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [price] => 65//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )//&nbsp;&nbsp;//&nbsp; &nbsp; &nbsp; [2] => Array//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [id] => 135//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [book_id] => 77//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [price] => 65//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )//&nbsp;&nbsp;//&nbsp; &nbsp; &nbsp; [3] => Array//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [id] => 100//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [book_id] => 78//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [price] => 65//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )//&nbsp;&nbsp;//&nbsp; &nbsp; &nbsp; [4] => Array//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [id] => 101//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [book_id] => 21//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [price] => 85//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )//&nbsp;&nbsp;//&nbsp; )
随时随地看视频慕课网APP
我要回答