【问题标题】:How to loop through an Json object with array in PHP如何在PHP中循环遍历带有数组的Json对象
【发布时间】:2020-03-24 03:53:30
【问题描述】:

我得到以下 JSON 格式的数据。

{"Students":
[{"ID":"600","datetime":"26-11-2019 04-32-31","age":"12"},
{"ID":"601","datetime":"26-11-2019 04-32-31","age":"13"},
{"ID":"602","datetime":"26-11-2019 04-32-31","age":"12"},
{"ID":"603","datetime":"26-11-2019 04-32-31","age":"14"}
]}

我想编写一个 PHP 脚本来在 MYSQL 中插入这些数据。

我不确定如何编写循环来遍历这些数据。

我尝试了以下代码,但它不起作用。

//encode the Json request.
$obj = json_decode($content);

foreach($obj as $key){
 // $email = $decode['RouteID'];

}

【问题讨论】:

  • 你能转储$key

标签: php json loops foreach


【解决方案1】:
$json = '{"Students":
[{"ID":"600","datetime":"26-11-2019 04-32-31","age":"12"},
{"ID":"601","datetime":"26-11-2019 04-32-31","age":"13"},
{"ID":"602","datetime":"26-11-2019 04-32-31","age":"12"},
{"ID":"603","datetime":"26-11-2019 04-32-31","age":"14"}
]}';
//convert json to array
$array = json_decode($json, true);

//loop
foreach($array['Students'] as $key => $val){
 echo "$key = $val \n"; 
}

//or print whole array
print_r($array);

print_r 的结果会是这样的:

(
    [Students] => Array
        (
            [0] => Array
                (
                    [ID] => 600
                    [datetime] => 26-11-2019 04-32-31
                    [age] => 12
                )

            [1] => Array
                (
                    [ID] => 601
                    [datetime] => 26-11-2019 04-32-31
                    [age] => 13
                )

            [2] => Array
                (
                    [ID] => 602
                    [datetime] => 26-11-2019 04-32-31
                    [age] => 12
                )

            [3] => Array
                (
                    [ID] => 603
                    [datetime] => 26-11-2019 04-32-31
                    [age] => 14
                )

        )

)

【讨论】:

    【解决方案2】:
    // creating connection to mysql
    $servername = "localhost";
    $username = "username";
    $password = "password";
    
    try {
        $conn = new PDO("mysql:host=$servername;dbname=DatbaseName", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Connected successfully";
        }
    catch(PDOException $e)
        {
        echo "Connection failed: " . $e->getMessage();
        }
    
    $content = '{"Students":
    [{"ID":"600","datetime":"26-11-2019 04-32-31","age":"12"},
    {"ID":"601","datetime":"26-11-2019 04-32-31","age":"13"},
    {"ID":"602","datetime":"26-11-2019 04-32-31","age":"12"},
    {"ID":"603","datetime":"26-11-2019 04-32-31","age":"14"}
    ]}';
    $data= json_decode($content);
    
    foreach($data->Students as $student){
    
    $sql = "INSERT INTO users (ID, datetime, age) VALUES (?,?,?)";
    $conn->prepare($sql)->execute([$student->ID, $student->datetime, $student->age]);
    
    }
    

    【讨论】:

      【解决方案3】:

      要遍历对象并获取其成员,您可以尝试以下操作:

      foreach($json as $obj){
         echo $obj->name;
         .....
      
      }
      

      使用$key 将不起作用!

      【讨论】:

        【解决方案4】:

        下面是简单的工作示例代码:-

        <?php
        $jsonData = '{"Students": [{"ID":"600","datetime":"26-11-2019 04-32-31","age":"12"},{"ID":"601","datetime":"26-11-2019 04-32-31","age":"13"},{"ID":"602","datetime":"26-11-2019 04-32-31","age":"12"},{"ID":"603","datetime":"26-11-2019 04-32-31","age":"14"}]}';
        
        $JD = json_decode($jsonData);
        
         // print_r($JD);
        
           foreach ($JD->Students as $key => $value) {
            echo $value->ID . ", " . $value->datetime . ", " . $value->age . "<br>";
            //print_r($value);
        }
        
        
        
         ?>
        

        【讨论】:

          【解决方案5】:
          $data = '{"Students":
          [{"ID":"600","datetime":"26-11-2019 04-32-31","age":"12"},
          {"ID":"601","datetime":"26-11-2019 04-32-31","age":"13"},
          {"ID":"602","datetime":"26-11-2019 04-32-31","age":"12"},
          {"ID":"603","datetime":"26-11-2019 04-32-31","age":"14"}
          ]}';
          
          $r = json_decode($data);
          
          
          foreach($r->Students as $key => $value)
          {
            //inser query
            $q = 'INSERT INTO <table_name>('ID','datetime','age') VALUES($value->ID,$value->datetime,$value->age);
          
          }
          

          【讨论】:

            【解决方案6】:

            您可以将 JSON 数据转换为 objectarray
            使用 php json_decode 函数来实现。

            转换为对象

            $data = '{"Students":
            [{"ID":"600","datetime":"26-11-2019 04-32-31","age":"12"},
            {"ID":"601","datetime":"26-11-2019 04-32-31","age":"13"},
            {"ID":"602","datetime":"26-11-2019 04-32-31","age":"12"},
            {"ID":"603","datetime":"26-11-2019 04-32-31","age":"14"}
            ]}';
            
            $obj = json_decode($data);
            
            foreach ($obj->Students as $key => $value) {
              echo $value->age;    
            }
            

            如果要将 JSON 数据转换为数组,请将第二个参数 (true) 设置为 json_decode。功能

            $data = '{"Students":
                [{"ID":"600","datetime":"26-11-2019 04-32-31","age":"12"},
                {"ID":"601","datetime":"26-11-2019 04-32-31","age":"13"},
                {"ID":"602","datetime":"26-11-2019 04-32-31","age":"12"},
                {"ID":"603","datetime":"26-11-2019 04-32-31","age":"14"}
                ]}';
            
                $array = json_decode($data, true);
            
                foreach ($array['Students'] as $key) {
                  echo $key['age'];    
                }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2023-03-07
              • 2016-07-28
              • 2021-12-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-06-27
              相关资源
              最近更新 更多