drgd73844 2014-05-19 23:30
浏览 35
已采纳

搜索数组以返回子值的键的最佳方法是什么

I'm trying to filter an array (derived from a json object), so as to return the array key based on the value. I'm not sure if array search $key = array_search($value, $array); is the best way to do this (I can't make it work), and I think there must be a better way.

So far I've got this, but it isn't working. Grateful for any help!

    public function getBedroomData(array $data,$num_beds = null,$type) {

    $data = (array) $data;

    if($num_beds > 0) {
        $searchstring = "avg_".$num_beds."bed_property_".$type."_monthly";
        } else {
        $searchstring = "avg_property_".$type."_monthly";
        }

        $avg_string = array_search($data, $searchstring);
    return $avg_string;             
    }

The array consists of average property prices taken from the nestoria api as follows: http://api.nestoria.co.uk/api?country=uk&pretty=1&action=metadata&place_name=Clapham&price_type=fixed&encoding=json

This returns a long json object. My problem is that the data isn't consistent - and I'm looking for the quickest (run time) way to do the following:

            $data['response']['metadata']['0'] //= data to return, [0] unknown
            $data['response']['metadata']['0']['metadata_name'] = "avg_1bed_property_rent_monthly" //= string I know!
            $data['response']['metadata']['1'] //= data to return, [1] unknown
            $data['response']['metadata']['1']['metadata_name'] = "avg_1bed_property_buy_monthly" //= string I know!
            $data['response']['metadata']['2'] = //= data to return, [2] unknown
            $data['response']['metadata']['2']['metadata_name'] = "avg_2bed_property_buy_monthly" //= string I know!
            .....
            .....
            .....
            $data['response']['metadata']['10'] = avg_property_rent_monthly
            $data['response']['metadata']['11'] = avg_property_buy_monthly
            $data['response']['metadata'][most_recent_month] = the month reference for getting the data from each metadata list.. 

It isn't possible to filter the initial search query by number of bedrooms as far as I can work out. So, I've just been array slicing the output to get the information I've needed if bedrooms are selected, but as the data isn't consistent this often fails.

  • 写回答

1条回答 默认 最新

  • drwghu6386 2014-05-20 10:57
    关注

    To search inside that particular json response from nestoria, a simple foreach loop can be used. First off, of course call the json data that you need. Then, extract the whole data, the the next step if pretty straightforward. Consider this example:

    $url = 'http://api.nestoria.co.uk/api?country=uk&pretty=1&action=metadata&place_name=Clapham&price_type=fixed&encoding=json';
    $contents = file_get_contents($url);
    $data = json_decode($contents, true);
    $metadata = $data['response']['metadata'];
    
    // dummy values
    $num_beds = 1; // null or 0 or greater than 0
    $type = 'buy'; // buy or rent
    
    function getBedroomData($metadata, $num_beds = null, $type) {
        $data = array();
    
        $searchstring = (!$num_beds) ? "avg_property_".$type."_monthly" : "avg_".$num_beds."bed_property_".$type."_monthly";
        $data['metadata_name'] = $searchstring;
        $data['data'] = null;
    
        foreach($metadata as $key => $value) {
            if($value['metadata_name'] == $searchstring) {
                $raw_data = $value['data']; // main data
    
                // average price and data points
                $avg_price = 0;
                $data_points = 0;
                foreach($raw_data as $index => $element) {
                    $avg_price += $element['avg_price'];
                    $data_points += $element['datapoints'];
                }
                $data_count = count($raw_data);
                $price_average = $avg_price / $data_count;
                $data_points_average = $data_points / $data_count;
    
                $data['data'][] = array(
                    'average_price' => $price_average,
                    'average_datapoints' => $data_points_average,
                    'data' => $raw_data,
                );
            }
        }
    
        return $data;
    }
    
    $final = getBedroomData($metadata, $num_beds, $type);
    
    print_r($final);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 爬虫爬取网站的一些信息
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错