如何在PHP中将此JSON请求转换为新的JSON输出?

如何在PHP中将此JSON请求转换为新的JSON输出?,php,html,json,Php,Html,Json,我正在尝试将一个JSON请求转换为一个新的JSON输出,因为我需要重命名一些正在使用的变量,以便在日历中工作。日历附带了一个输出JSON的PHP设置示例 如何让SAMPLE.PHP将数据作为循环进行回显,将JSON文件中的变量名转换为相同的名称 SAMPLE.PHP=用于设置从Jquery调用的日历JSON的文件 <?php $year = date('Y'); $month = date('m'); echo json_encode(array( array(

我正在尝试将一个JSON请求转换为一个新的JSON输出,因为我需要重命名一些正在使用的变量,以便在日历中工作。日历附带了一个输出JSON的PHP设置示例

如何让SAMPLE.PHP将数据作为循环进行回显,将JSON文件中的变量名转换为相同的名称

SAMPLE.PHP=用于设置从Jquery调用的日历JSON的文件

<?php

$year = date('Y');
$month = date('m');

echo json_encode(array(

    array(
        'id' => 111,
        'title' => "Event1",
        'start' => "$year-$month-10",
        'url' => "http://www.eventurl.com/1"
    ),

    array(
        'id' => 222,
        'title' => "Event2",
        'start' => "$year-$month-20",
        'end' => "$year-$month-22",
        'url' => "http://www.eventurl.com/2"
    ),

));

?>
MYJSON=我想从自己的JSON url中提取自己的数据,而不是从示例PHP创建中提取数据(但保留示例PHP结构)。这是我的JSON请求的结果,我删除了许多其他行,只是为了清理这篇文章中的混乱(myUrl.JSON):

{
"events": [
    {
        "event": {

            "id": 1164038671,
            "title": "TestEvent",
            "start_date": "2011-06-24 13:00:00",
            "end_date": "2011-06-24 16:00:00",
            "url": "ttp://www.eventurl.com/1",

        }
    },
    {
        "event": {

            "id": 1163896245,
            "title": "Test",
            "start_date": "2011-07-14 13:00:00",
            "end_date": "2011-07-14 16:00:00",
            "url": "ttp://www.eventurl.com/2",

        }
    }
]
}
<?php 

$string = file_get_contents('myUrl.json');
$json_a=json_decode($string,true);

// array method
foreach($json_a[events] as $p)
{
echo '
ID: '.$p[event][id].' <br/>
TITLE: '.$p[event][title].' <br/>
START DATE: '.$p[event][start_date].' <br/>
END DATE: '.$p[event][end_date].' <br/>
URL: '.$p[event][url].' <br/>

<br/><br/>
';

}
?>
作为一项测试,我能够检索这样的值,以便显示:

{
"events": [
    {
        "event": {

            "id": 1164038671,
            "title": "TestEvent",
            "start_date": "2011-06-24 13:00:00",
            "end_date": "2011-06-24 16:00:00",
            "url": "ttp://www.eventurl.com/1",

        }
    },
    {
        "event": {

            "id": 1163896245,
            "title": "Test",
            "start_date": "2011-07-14 13:00:00",
            "end_date": "2011-07-14 16:00:00",
            "url": "ttp://www.eventurl.com/2",

        }
    }
]
}
<?php 

$string = file_get_contents('myUrl.json');
$json_a=json_decode($string,true);

// array method
foreach($json_a[events] as $p)
{
echo '
ID: '.$p[event][id].' <br/>
TITLE: '.$p[event][title].' <br/>
START DATE: '.$p[event][start_date].' <br/>
END DATE: '.$p[event][end_date].' <br/>
URL: '.$p[event][url].' <br/>

<br/><br/>
';

}
?>

为什么不解码传入的JSON,将其处理成您想要的,然后编码JSON并打印出来

如果需要重命名变量(或键),请设置
$tmp
变量。 就是

所以,我想您的代码应该是这样的:

<?php 

    $string = file_get_contents('myUrl.json');
    $json_a=json_decode($string,true);
    $json_b = array();

    // array method
    foreach($json_a[events] as $p)
    {
        $json_b[event]['ID'] = $json_a[event][id];
        $json_b[event]['TITLE'] = $json_a[event][title];
        //and so on...
    }

    echo json_encode($json_b);

?>