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


JSON search event names and list URL values

我正在从URL:加载事件列表作为JSON文件

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

直接输出如下(更多值和mor行):

    [{"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()等不同的东西,但没有成功。

感谢您的帮助!

尝试这个

<?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'];
        }
    }
?>

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

<?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;
        }
    }
?>

任何一种都应该同样有效。