dsjuimtq920056 2013-09-25 09:10
浏览 33
已采纳

如何使用子值对多个数组进行排序

i have array which contains hotels arrays,

  • every (hotel) array have (rooms) array that contain rooms data like the price for this room and room number.
  • i want to get the five hotels with the lowest rooms price that called (price_str)

important notes :

Number of arrays for hotels unexpected

Number of arrays for rooms unexpected

i work in this project with php 5.3..so some new php features not work

// the first hotel info array
    id:52548
    Le Meridien Heliopolis
    51 Al Orouba Street Heliopolis  Po Box 2928
    stars 5

    rooms 255
//description
    Set in one of the finest residential districts of Cairo, the Le Meridien Heliopolis
    is ideally located near the Cairo International Conference Centre,
    // image
    http://cdn.wego.com/gazo/88/2f1ceaef3b36d83d13bb01b0f413298bb2e42514/137974_B.jpg
// this array contains rooms info in this hotel
    Array
    (
        [0] => stdClass Object
            (
                [id] => 119-9
                [price_str] => 95
                [currency_code] => USD
                [currency_sym] => US$
                [provider_name] => Le Meridien
                [provider_code] => lemeridien.com
                [ex_tax] => 1
                [is_direct] => 1
            )

        [1] => stdClass Object
            (
                [id] => 6-1
                [price_str] => 104
                [currency_code] => USD
                [currency_sym] => US$
                [provider_name] => Booking.com
                [provider_code] => booking.com
                [ex_tax] => 
                [is_direct] => 
            )

        [2] => stdClass Object
            (
                [id] => 88-201
                [price_str] => 104
                [currency_code] => USD
                [currency_sym] => US$
                [provider_name] => Wego
                [provider_code] => wego.com
                [ex_tax] => 
                [is_direct] => 
            )

    )

// the second hotel info
    id:52629
    Holidays Express Hotel
    2 Gameat El Dowal El Arabyia St.,Sphinx Square , Mohandessin 
    stars 4
    rooms 
// desciption
    With its central location, Holidays Express Hotel is within easy reach of most
    tourist attractions and business addresses in Cairo.
// image
    http://cdn.wego.com/gazo/43/78be2c05a7069febbfc15b88d40af58cc8871751/7350810_B.jpg

// the rooms info for this hotel
    Array
    (
        [0] => stdClass Object
            (
                [id] => 6-1
// i want to sort by lowest price_str
                [price_str] => 63
                [currency_code] => USD
                [currency_sym] => US$
                [provider_name] => Booking.com
                [provider_code] => booking.com
                [ex_tax] => 
                [is_direct] => 
            )

        [1] => stdClass Object
            (
                [id] => 88-201
                [price_str] => 63
                [currency_code] => USD
                [currency_sym] => US$
                [provider_name] => Wego
                [provider_code] => wego.com
                [ex_tax] => 
                [is_direct] => 
            )

        [2] => stdClass Object
            (
                [id] => 70-1
                [price_str] => 69
                [currency_code] => USD
                [currency_sym] => US$
                [provider_name] => DHR.com
                [provider_code] => dhr.com
                [ex_tax] => 1
                [is_direct] => 
            )

    )
  • 写回答

1条回答 默认 最新

  • dqwh1202 2013-09-25 09:27
    关注
    • find the lowest price of each hotel, store the price in a array, index by hotel index.
    • sort the array by price.
    • fetch the top 5 result

    #

    <?php
    $price_list = array(); // the lowest price of each hotel stroes in this array, indexed by the index of each hotel in $hotels array
    
    foreach ($hotels->json_data->hotels as $hotel_index => $hotel)
    {
        // find out the lowest price of this hotel
        $min_price = 0;
        foreach ($hotel->room_rates as $room)
        {
            if ($min_price == 0 || $room->price_str < $min_price)
            {
                $min_price = $room['price_str'];
            }
        }
        $price_list[$hotel_index] = $min_price;
    }
    
    // sort by price
    asort($price_list, SORT_NUMERIC);
    
    // fetch the top hotel
    $count = 0;
    $list = array();
    foreach ($price_list as $index => $price)
    {
        $list[] = $hotels->json_data->hotels[$index];
        $count++;
        if ($count > 5)
        {
            break;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里