dpsx99068 2015-03-10 16:44
浏览 101
已采纳

too long

I'd like to be able to search for the array with the title Seattle, which will be set by a variable. Then return the x or y coordinate for that array. I've tried 5 or 6 different ways of trying to locate it without any luck.

This is the query I am using and how I am printing my array:

global $wpdb;
$myquery = $wpdb->get_row("SELECT * FROM wp_maps WHERE title = 'Test Map'"); 
$mymap =  $mylink->data;

print_r($mymap);

This is the actual output.

{ "title":"USA", "location":"World", "levels":[ { "id":"states", "title":"States", "locations":[{"id":"bhAAG","title":"Seattle","description":"The City of Goodwill","x":47.6097,"y":122.3331},{"id":"biAAG","title":"Portland","description":"Portland, Maine. Yes. Life’s good here.","x":43.6667,"y":70.2667}] } ] }

Same output (formatted for easier viewing).

{
    "title":"USA",
    "location":"World",
    "levels":[
        {
            "id":"states",
            "title":"States",
            "locations":[
                {
                    "id":"bhAAG",
                    "title":"Seattle",
                    "description":"The City of Goodwill",
                    "x":47.6097,
                    "y":122.3331
                },
                {
                    "id":"biAAG",
                    "title":"Portland",
                    "description":"Portland, Maine. Yes. Life’s good here.",
                    "x":43.6667,
                    "y":70.2667
                }
            ]
        }
    ]
}

Any help would be appreciated.

  • 写回答

2条回答 默认 最新

  • donglu7286 2015-03-10 17:43
    关注

    Your myMap data is in JSON format. You can json_decode it into an array and then search all locations for an array with the specified title:

    $myMap = '{ "title":"USA", "location":"World", "levels":[ { "id":"states", "title":"States", "locations":[{"id":"bhAAG","title":"Seattle","description":"The City of Goodwill","x":47.6097,"y":122.3331},{"id":"biAAG","title":"Portland","description":"Portland, Maine. Yes. Life’s good here.","x":43.6667,"y":70.2667}] } ] }';
    
    // Convert JSON and grab array of locations
    $array     = json_decode($myMap, true);
    $locations = $array['levels'][0]['locations'];
    
    // What we are looking for
    $title = 'Seattle';
    
    // Search locations
    foreach ($locations as $location) {
        if ($location['title'] == $title) {
            $x = $location['x'];
            $y = $location['y'];
        }
    }
    
    echo "x = $x, y = $y", PHP_EOL;
    

    Output:

    x = 47.6097, y = 122.3331
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!