JSON搜索事件名称和列表URL值

时间:2015-11-17 17:31:00

标签: php arrays json multidimensional-array

我将事件列表从URL加载为JSON文件:

$file = file_get_contents('http://ecample.com/listing.php?'); 
$data = json_decode($file); 
?>

直接输出看起来像这样(更多值和时间线):

    [{"id":"1","name":"NAME_1","booking_url":"https://ecample.com/Event_ID65654","category_id":"195"},
    {"id":"2","name":"NAME_2","booking_url":"https://ecample.com/Event_ID65654","category_id":"195"},
    "id":"1","name":"NAME_1","booking_url":"https://ecample.com/Event_ID65654","category_id":"195"}]

我需要搜索所有值为" name":" NAME_1"并打印出" booking_url"的值。

我尝试了array_seach()之类的其他内容,但没有成功。

感谢任何帮助!

2 个答案:

答案 0 :(得分:2)

试试这个

<?php

    $file = file_get_contents('http://ecample.com/listing.php?'); 
    $data = json_decode($file, true);

    foreach ($data as $r)
    {
        if ($r['name'] == 'NAME_1')
        {
            echo $r['booking_url'];
        }
    }

?>

答案 1 :(得分:0)

另一个选项是在对象上下文中访问您的数据,因此:

<?php

    $file = file_get_contents('http://ecample.com/listing.php?'); 
    $data = json_decode($file);

    foreach ($data as $r)
    {
        if ($r->name == 'NAME_1')
        {
            echo $r->booking_url;
        }
    }

?>

任何一个都应该也能正常工作。