用PHP解析JSON中的多维数组



我需要从外部JSON文件中获取数据,并使用PHP解析到MySQL。

非常令人沮丧,一直在工作,但我只是未能做到这一点,我真的很感激你的帮助。

我可以插入数据MYSQL自己,但我需要帮助,请在准备它。

外部JSON文件结构的示例。

[
  {
    league_id: "3",
    league_name: "League "3",
    teams: []
  },
  {
    league_id: "2",
    league_name: "League 2",
    team: [
      {
        name_of_team: "Brixton United",
        number_of_players: "28",
        homeground: "Brockewell Park",
        history: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.",
        nextmatchtime: 1405046700
      }
      ]
      },
     {
    league_id: "1",
    league_name: "League 1",
    team: [
      {
        name_of_team: "Croydon Crusaders",
        number_of_players: "26",
        homeground: "North End Road",
        history: "Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum.",
        nextmatchtime: 1402458300
      },
      {
        name_of_team: "Streatham Skaters",
        number_of_players: "31",
        homeground: "Streatham Common",
        history: "Curabitur sit amet mauris. Morbi in dui quis est pulvinar ullamcorper. Nulla facilisi. Integer lacinia sollicitudin massa. Cras metus. Sed aliquet risus a tortor. Integer id quam. Morbi mi. Quisque nisl felis, venenatis tristique, dignissim in, ultrices sit amet, augue. Proin sodales libero eget ante. Nulla quam. Aenean laoreet. Vestibulum nisi lectus, commodo ac, facilisis ac, ultricies eu, pede. Ut orci risus, accumsan porttitor, cursus quis, aliquet eget, justo.",
        nextmatchtime: 1433990700
      }
     ]
      }
      ]

我被PHP困住了,并试图回显它以测试脚本输出,以便稍后将其插入MYSQL。

嵌套的内部数组必须继承外部数组key/value,因为不会插入任何外部(联盟)数组。只有内部数组(teams)会被插入。

    <?php
        //create a DB connection
        $con=mysqli_connect("localhost","wordpress1","*****","wordpress1");

        $my_arr = file_get_contents("http://example.com/updating/json/data.php");
        $ujson = json_decode($my_arr, true);


   // Inner array and outer array inherited fields, to be inserted in to MYSQL later.
      foreach ($ujson as $key => $value)
        {

    if (is_array($value))
  foreach ($value as $key2 => $value2)
    {
                    echo $value['name_of_team'];
            echo "</br>";
            echo $value['number_of_players'];
            echo "</br>";
                    echo $value['homeground'];
            echo "</br>";
            echo $value['history'];
            echo "</br>";
                    echo $value['nextmatchtime'];
            echo "</br>";
   }
  else
   {
                    echo $value['league_id'];
            echo "</br>";
            echo $value['league_name'];
            echo "</br>";
   }
    }  
    mysqli_close($con);
    ?>

作为代码,它有无效的参数为第二个foreach函数,但我毫不怀疑,这并不是所有的错误与它。

这是你探索坏数组的方式。像这样试试:

foreach ($ujson as $league)
{
    var_dump($league['league_id']);
    // also league_name...
    if ( !empty($league['teams'])
    {
        foreach( $league['teams'] as $team )
        {
            var_dump($team['name_of_team']);
            // also number_of_players...
        }
    }
}

我不确定,但试试这个

写下面的代码

if(!empty($value))
不是

if (is_array($value))

我已经分析了您的JSON文件内容。JSON内容中的第一个数组包含无效数据。即:
[{league_id:"3",联赛名称:"联赛"3",团队:[]},……)

代替:

({league_id:"3",联赛名称:"联赛3";团队:[]},……)

图例名称的值中包含双引号(")。这就是为什么脚本无法将JSON数据转换为数组的原因。

请删除双引号或用另一个字符替换。

相关内容

  • 没有找到相关文章