dongsui5464 2014-03-21 21:34
浏览 195
已采纳

通过PHP中的数组键的值对字典中的数组进行排序

This is sort of a two part question, but here's my PHP code below and I want to sort the business_hours by day in ascending order (0,1,...6). Is this easier in PHP or iOS (this is written for integration into an iOS app)?

Also, sidenote, my iOS developer says he has issues when returning the locations array dictionary like the one below. He'd rather have locations as a numbered array (JSON for [{},{},...] instead of what I have as {},{},...), but the issue is I simply can't find a way in PHP to do that for this application requirement. I specifically need to use array keys for adding the business hours to its corresponding location. I am doing a join of three tables to grab business hours and location id, so that the location id for the business hours matches the location id of the location itself; that seems to be the only way to get the two arrays to join for the JSON output array to work... You can see below, but let me know if I am wrong or if it would be easier for my iOS developer to just learn how to iterate through and return all array values for a multidimensional, associative array with keys. Please advise!

if ($stmt = $dbh->prepare($query)) {
            // initialise an array for the results 
            $result = array();
            if ( $stmt->execute(array($lat,$lat,$lng,$rest_price,$eat_in,$take_out,$delivery)) ) {
                        // loop through all values
                while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
                    // Have we seen this menu before? If not, add it to the array
                    if ( !isset($result['locations'][$row['rest_id']]) ) {
                        $result['locations'][$row['rest_id']] = array(
                            'rest_id' => $row['rest_id'],
                            'user_id' => $row['user_id'],
                            'rest_name' => $row['rest_name'],
                            'lat' => $row['lat'],
                            'lng' => $row['lng'],
                            'rest_price' => $row['rest_price'],
                            'rest_rating' => $row['rest_rating'],
                            'rest_genre' => $row['rest_genre'],
                            'eat_in' => $row['eat_in'],
                            'take_out' => $row['take_out'],
                            'delivery' => $row['delivery'],
                            'rest_img' => $row['rest_img'],
                            'user_img' => $row['user_img'],
                            'business_hours' => array()
                        );
                    }
                    // Add the ingredient.
                    // remove all NULL, FALSE and Empty Strings but leave 0 (zero) values
                    $result['locations'][$row['rest_id']]['business_hours'][] = array_filter(array(
                        'day' => $row['day'],
                        'open_time' => $row['open_time'],
                        'close_time' => $row['close_time']
                    ), 'strlen');
                }

                // print results if not null
                if( $result != null ) {       
                    // print success. no error.
                    $result['error'] .= '';     
                    echo json_encode($result);
                    //print_r($result);
                } else {
                    echo json_encode(array('error' => 'No locations exist in your area'));
                }

Array

(
[locations] => Array
    (
        [67] => Array
            (
                [rest_id] => 67
                [user_id] => 19
                [rest_name] => The Ninja
                [lat] => 34.1516
                [lng] => -106.685591
                [rest_price] => 2
                [rest_rating] => 3.5
                [rest_genre] => Japanese
                [eat_in] => 1
                [take_out] => 1
                [delivery] => 1
                [rest_img] => 88/image11.png
                [user_img] => image595.png
                [business_hours] => Array
                    (
                        [0] => Array
                            (
                                [day] => 4
                                [open_time] => 09:00:00
                                [close_time] => 16:30:00
                            )

                        [1] => Array
                            (
                                [day] => 1
                                [open_time] => 10:00:00
                                [close_time] => 17:00:00
                            )

                        [2] => Array
                            (
                                [day] => 6
                                [open_time] => 12:00:00
                                [close_time] => 18:00:00
                            )

                        [3] => Array
                            (
                                [day] => 3
                                [open_time] => 10:00:00
                                [close_time] => 17:00:00
                            )

                        [4] => Array
                            (
                                [day] => 0
                                [open_time] => 00:00:00
                                [close_time] => 00:00:00
                            )

                        [5] => Array
                            (
                                [day] => 5
                                [open_time] => 10:00:00
                                [close_time] => 17:00:00
                            )

                        [6] => Array
                            (
                                [day] => 2
                                [open_time] => 10:00:00
                                [close_time] => 17:00:00
                            )

                    )

            )// more arrays occur after this...

    )

[error] => 
)

JSON

{"locations":{"67":{"rest_id":"67","user_id":"19","rest_name":"The Ninja","lat":"","lng":"","rest_price":"2","rest_rating":"3.5","rest_genre":"Japanese","eat_in":"1","take_out":"1","delivery":"1","rest_img":"","user_img":"","business_hours":[{"day":"6","open_time":"12:00:00","close_time":"18:00:00"},{"day":"3","open_time":"10:00:00","close_time":"17:00:00"},{"day":"0","open_time":"00:00:00","close_time":"00:00:00"},{"day":"5","open_time":"10:00:00","close_time":"17:00:00"},{"day":"2","open_time":"10:00:00","close_time":"17:00:00"},{"day":"4","open_time":"09:00:00","close_time":"16:30:00"},{"day":"1","open_time":"10:00:00","close_time":"17:00:00"}]},{},...},"error":""}
  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥100 AZURE创建地图类webapp
      • ¥20 51单片机实训实验报告
      • ¥15 C# 循环读写数据中途突然变慢
      • ¥15 用Java实现双端队列
      • ¥150 ID3决策树实现分类
      • ¥15 multisim10安装后,找不到NI License Manager的程序来安装许可证
      • ¥15 C++银行卡系统 Help!
      • ¥15 R语言数据分析的相关问题
      • ¥15 模型导入SP后贴图纹理只有一个,拆了四张UV的,怎么解决?
      • ¥15 检索带order by 非常慢