Json额外的领域

时间:2015-07-17 15:14:28

标签: php arrays json api

我创建了这个工作代码,它将数据从api存储到json文件  基于昨天的日期。

$filedate = date('Y-m-d',strtotime("-1 days"));
    $yesterday = date('Y-m-d 00:00',strtotime("-1 days"));
    $today = date('Y-m-d 23:59',strtotime("-1 days"));

    try{

        $soccer=new XMLSoccer("API KEY");
        $soccer->setServiceUrl("http://www.xmlsoccer.com/FootballData.asmx");
        /*  $result=$soccer->GetLiveScore();*/
        $result=$soccer->GetFixturesByDateInterval(array( "startDateString"=> "$yesterday" ,"endDateString"=> "$today"));            
        var_dump($result);

        $fp = fopen( "/var/www/public_html/domain/$filedate.json","w+");
        fwrite($fp,json_encode($result));
        fclose($fp);
    }
    catch(XMLSoccerException $e){
        echo "XMLSoccerException: ".$e->getMessage();
    }

上面的代码将此json数据存储在文件中:

{
Match: [
{
Id: "346528",
Date: "2015-07-15T12:00:00+00:00",
League: "Chinese Super League",
Round: "19",
HomeTeam: "Guangzhou Evergrande",
HomeTeam_Id: "1108",
HomeGoals: "0",
AwayTeam: "Henan Jianye",
AwayTeam_Id: "1100",
AwayGoals: "0",
Time: "Not started",
Location: "Tianhe Stadium",
HomeTeamYellowCardDetails: { },
AwayTeamYellowCardDetails: { },
HomeTeamRedCardDetails: { },
AwayTeamRedCardDetails: { }
},
{
Id: "346527",
Date: "2015-07-15T11:45:00+00:00",
League: "Chinese Super League",
Round: "19",
HomeTeam: "Shanghai Greenland Shenhua",
HomeTeam_Id: "1107",
HomeGoals: "0",
AwayTeam: "Beijing Guoan",
AwayTeam_Id: "1098",
AwayGoals: "0",
Time: "Not started",
Location: "Hongkou Football Stadium",
HomeTeamYellowCardDetails: { },
AwayTeamYellowCardDetails: { },
HomeTeamRedCardDetails: { },
AwayTeamRedCardDetails: { }
},
}

现在我需要为每个匹配添加一些额外的字段,例如,如果HomeTeam =皇家马德里,添加额外的字段HomeTeamShort = RMD等。这应该动态完成,因为我必须为每个匹配创建IF' s和主队。

有人可以帮我怎么做?

2 个答案:

答案 0 :(得分:0)

首先你需要解码你的json

$array = json_decode($result);

获取Match数组

$arr = $array->Match;

现在遍历所有对象(即)并做任何必要的事情

foreach($arr as $item) { //foreach element in $arr
    //do whatever you want to do with that object
    if($item->HomeTeam == "Real Madrid") {
       $item->HomeTeamShort = "RMD";
    }
}

print_r($arr); //Now $arr is the required result you wanted to have

//you can also convert your array back json 
$arr2 = json_encode($arr);

如果您想要其他内容,请告诉我,我会更改我的代码以满足您的需求。

谢谢。

*附加帮助示例*

在运行循环之前,检查是否已正确放置对象的变量和数组。由于我无法看到您在代码中所做的事情以及您的行号是什么,因此我会将完整的虚拟代码与输出粘贴在一起,请将其与您自己的代码进行比较,您应该能够获得正确的输出

<?php
//let's take an example json data
$result = '{"Match":
                [
                    {"name":"Tom", "age":22},
                    {"name":"Jerry", "age":20},
                    {"name":"Hank", "age":25}
                ]           
            }';

$array = json_decode($result);
$arr = $array->Match;

让我们在处理之前打印一次数组

print_r($arr); //Array ( [0] => stdClass Object ( [name] => Tom [age] => 22 ) [1] => stdClass Object ( [name] => Jerry [age] => 20 ) [2] => stdClass Object ( [name] => Hank [age] => 25 ) )

现在执行循环操作并将所需的任何属性添加到您想要的任何对象

foreach($arr as $item) { //foreach element in $arr
    //do whatever you want to do with that object
    if($item->name == "Jerry") {
       $item->color = "Brown";
    }
}

现在,如果再次打印数组,您会看到 Color Brown已添加到Jerry

print_r($arr); //Array ( [0] => stdClass Object ( [name] => Tom [age] => 22 ) [1] => stdClass Object ( [name] => Jerry [age] => 20 [color] => Brown ) [2] => stdClass Object ( [name] => Hank [age] => 25 ) )

如果需要,请将您的数组转换回json

$arr2 = json_encode($arr);

现在,检查一下json,你还会看到 Color Brown 那里对抗Jerry

var_dump($arr2); //string(92) "[{"name":"Tom","age":22},{"name":"Jerry","age":20,"color":"Brown"},{"name":"Hank","age":25}]"

?>

这是对此代码的逐步分析和解释。如您所见,我没有得到任何未定义的对象错误。因此,尝试复制粘贴我的上述代码,如果您可以复制相同的错误,那么我很容易理解导致您的错误的原因。

我还制作了PHPFiddle Link here,您可以检查代码并在那里运行。

再次感谢。

答案 1 :(得分:0)

感谢您的帮助,不要解决我的问题,但要让我取得进步(我已经接近了)

也许这是我的错误,因为我没有提供一些重要的事情

所以..我在phpfiddle上测试你的代码并且它可以正常工作,主要是因为你直接在代码上有json,在我的项目中我需要从服务器打开file.json,并且你的代码甚至可以调整一些事情,我得到一些错误,所以......我改变了你的代码:

    <?php

$json_url = "http://getinstagoal.com/2015-07-20.json";
    $json = file_get_contents($json_url);
    $links = json_decode($json, TRUE);

foreach($links["Match"] as $key=>$val) {
        if($val['HomeTeam'] == "Palmeiras") {

            $val['HomeTeamshor'] = "RMD";

    }
}




//you can also convert your array back json 
$arr2 = json_encode($links);

//print the json, you will also see the Color Brown there against Jerry
var_dump($arr2); //string(92) "[{"name":"Tom","age":22},{"name":"Jerry","age":20,"color":"Brown"},{"name":"Hank","age":25}]"


?>

使用该代码我能够获得正确的数据,我尝试使用一些回声等等。

然而,即使在服务器上以root身份运行php,文件更新也不起作用,我检查并且&#34;如果&#34;工作(我用&#34; echo&#34测试它)我尝试使用file_put_content没有工作,我成功地向json文件添加内容的唯一方法是创建内容的数组,但这将重写整个文件。有什么想法吗?

其他事情,我需要做&#34;如果&#34;每场比赛8项,如

if($val['HomeTeam'] == "Real Madrid") {

            $val['HomeTeamshort'] = "RMD";
 }

if($val['AwayTeam'] == "Barcelona") {

            $val['AwayTeamshort'] = "FCB";
 }

超过1300(它应该在文件中进行比较,通常是每天20场比赛,而不是那么多)

我必须做的最有效的方法是什么&#34; ifs&#34;每场比赛?

谢谢@Suman