James Hickling
James Hickling

Reputation: 119

How to loop through an Json object with array in PHP

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

}

Upvotes: 0

Views: 2234

Answers (6)

BCM
BCM

Reputation: 675

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

Upvotes: 0

zemrin
zemrin

Reputation: 27

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

}

Upvotes: 0

Ripa Saha
Ripa Saha

Reputation: 2540

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



 ?>

Upvotes: 0

jagad89
jagad89

Reputation: 2643

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

}

Upvotes: 0

Akam
Akam

Reputation: 1052

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

        )

)

Upvotes: 1

Enrico Cortinovis
Enrico Cortinovis

Reputation: 871

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!

Upvotes: 0

Related Questions