如何使用这种格式在php中传递json文件

时间:2017-11-10 11:16:27

标签: php arrays json

我有这个json文件

[
    ["TITLE", "CONTENT", "DATE"],
    ["Monday", "Content of the monday post goes here", "09:55\n10:00\n10:30\n11:00\n15:45"],
    ["Tuesday", "Content of the tuesday day goes here", "08:00\n11:00\n16:00\n16:00\n21:00\n"],
]

我使用json_decode(file_get_contents))

对其进行解码

但是我如何才能获得星期一的标题?

这是我尝试的代码

$days = json_decode(file_get_contents('json_file_link'));
        foreach($days as $item){
                if(isset($item)){

                    $a = $item->Monday;
                }
            };
echo $a;

4 个答案:

答案 0 :(得分:0)

尝试以下代码

$days = json_decode(file_get_contents('json_file_link'));
        foreach($days as $item){
                if(isset($item)){

                    $a = $item->TITLE;
                }
            }
echo $a;

答案 1 :(得分:0)

您的JSON无效JSON - 额外的“,”最终会破坏事物。 json_decode()上的$days = json_decode('[ ["TITLE", "CONTENT", "DATE"], ["Monday", "Content of the monday post goes here", "09:55\n10:00\n10:30\n11:00\n15:45"], ["Tuesday", "Content of the tuesday day goes here", "08:00\n11:00\n16:00\n16:00\n21:00\n"] ]'); foreach ($days as $day) { if ($day[0] === 'Monday') { echo 'Monday title is ' . $day[1]; } } 将返回NULL,as demonstrated in this PHPFiddle

修复后,以下是访问元素的方法:

@media only screen and (min-width:450px){
  .dark-blue{
    width: 25%;
  }
  .blue{
    width: 75%;
  }
}

答案 2 :(得分:0)

我进行了一些实验,看起来数据是以数组格式显示的,我可以使用这个合成器获取数据

foreach($days as $item){
            if(isset($item)){
                $columns = $item;
            }
            echo $columns[0];

        };

答案 3 :(得分:0)

您的JSON无效......

<?php
    $json = '[["TITLE", "CONTENT", "DATE"],["Monday", "Content of the monday post goes here", "09:55\n10:00\n10:30\n11:00\n15:45"],["Tuesday", "Content of the tuesday day goes here", "08:00\n11:00\n16:00\n16:00\n21:00\n"]]';
    foreach((json_decode($json,true)) as $item){
        if(isset($item)){
            echo  $a = $item[0].'<br>'; //This have your values...
        }
    }
?>