0

I get the following data in JSON format.

{"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"}
]}

I want to write a PHP script to insert this data in MYSQL.

I'm not sure how to write the Loop to go through this data.

I tried the following code, and it does not work.

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

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

}
1
  • Can you dump $key Commented Nov 28, 2019 at 13:16

6 Answers 6

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);

The result of print_r will be like this:

(
    [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
                )

        )

)
Sign up to request clarification or add additional context in comments.

Comments

0
// 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]);

}

Comments

0

To loop through the object and get its members you can try this:

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

}

Using $key won't work!

Comments

0

Below is the simple working example code:-

<?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);
}



 ?>

Comments

0
$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);

}

Comments

0

You can convert your JSON data to object or array.
Use php json_decode function to do it.

Convert to object

$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;    
}

If you want convert JSON data to array put second argument (true) to json_decode. function

$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'];    
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.