【问题标题】:return value by searching for title inside a multidimensional array通过在多维数组中搜索标题返回值
【发布时间】:2015-05-12 05:24:01
【问题描述】:

我希望能够搜索标题为 Seattle 的数组,该数组将由一个变量设置。然后返回该数组的 x 或 y 坐标。我已经尝试了 5 或 6 种不同的方法来尝试定位它,但没有任何运气。

这是我正在使用的查询以及我如何打印我的数组:

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

print_r($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}] } ] }

相同的输出(格式化以便于查看)。

{
    "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
                }
            ]
        }
    ]
}

任何帮助将不胜感激。

【问题讨论】:

  • 使用 print_r($mymap) 的实际输出进行编辑
  • 我已经添加了上面的实际输出。

标签: php arrays variables multidimensional-array


【解决方案1】:

您的 myMap 数据采用 JSON 格式。您可以将json_decode 放入一个数组中,然后在所有位置中搜索具有指定标题的数组:

$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;

输出:

x = 47.6097, y = 122.3331

【讨论】:

  • 效果很好,这正是我所要求的。你是男人,谢谢你的帮助。看起来我没有得到每个位置的正确位置,但你把它钉牢了。再次感谢。
【解决方案2】:

紧凑的解决方案PHP5 >= 5.3

$term = ''; // term being used to search
if(isset($mymap['levels']) && isset($mymap['levels']['locations'])){
    $locations = $mymap['levels']['locations'];

    // filtered will be an array of x, y values
    $filtered = array_map(function($location){
        return [ 'x' => $location['x'], 'y' => $location['y']]; // transform into required format
    }, array_filter($locations, function($location) use ($term){ // filter by title
        return $location['title'] === $term;
    }));
}

array_filter() array_map()

【讨论】:

  • 我已经尝试过了,但是我收到了这个错误,解析错误:语法错误,意外的'{'。里面有多余的角色吗?
  • @IYBITWC 哎呀,对不起。修复。如果你真的需要这个,请回来查看。
  • 当我回到这里时将测试并发布更新。
猜你喜欢
  • 1970-01-01
  • 2013-08-29
  • 1970-01-01
  • 1970-01-01
  • 2014-05-20
  • 2017-11-16
  • 2019-10-16
  • 2014-04-16
  • 2018-06-30
相关资源
最近更新 更多