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 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类