2017-03-28 2 views
0

Ich möchte meine JSON-Daten zu MySQL DB einfügen PHP ...Insert JSON in mehrere Array-Daten in MySQL mit PHP

Diese meine JSON-Daten genannt ist "data.json"

{ 
"allRoundData": [{ 
    "name": "Animals", 
    "timeLimitInSeconds": 20, 
    "pointsAddedForCorrectAnswer": 10, 
    "questions": [{ 
     "questionText": "Lions are carnivores: true or false?", 
     "answers": [{ 
      "answerText": "True", 
      "isCorrect": true 
     }, { 
      "answerText": "False", 
      "isCorrect": false 
     }] 
    }, { 
     "questionText": "What do frogs eat?", 
     "answers": [{ 
      "answerText": "Pizza", 
      "isCorrect": false 
     }, { 
      "answerText": "Flies", 
      "isCorrect": true 
     }] 
    }, { 
     "questionText": "Where do mice live?", 
     "answers": [{ 
      "answerText": "In the sea", 
      "isCorrect": false 
     }, { 
      "answerText": "On the moon", 
      "isCorrect": false 
     }, { 
      "answerText": "On land", 
      "isCorrect": true 
     }, { 
      "answerText": "In a tree", 
      "isCorrect": false 
     }] 
    }] 
}] 

}

Das ist mein PHP-Skript

<?php 

include 'bl_Common.php'; 

$con = dbConnect(); 

// use prepare statement for insert query 
$st = mysqli_prepare($con, 'INSERT INTO data (round_name, time_limit, points_added, question_text, answer_text, isCorrect) VALUES (?, ?, ?, ?, ?, ?)'); 

// bind variables to insert query params 
mysqli_stmt_bind_param($st, 'siisss', $name, $time, $points, $question, $answer, $isCorrect); 

// read json file 
$filename = 'data.json'; 
$json = file_get_contents($filename); 

echo $json; 

//convert json object to php associative array 
$data = json_decode($json, true); 

// loop through the array 
foreach ($data as $row) { 

    // get the employee details 
    $name = $row["allRoundData"]["name"]; 
    $time = $row['allRoundData']['timeLimitInSeconds']; 
    $points = $row['allRoundData']['pointsAddedForCorrectAnswer']; 
    $question = $row['allRoundData']['questions']['questionText']; 
    $answer = $row['allRoundData']['answers']['answerText']; 
    $isCorrect = $row['allRoundData']['answers']['isCorrect']; 

    // execute insert query 
    mysqli_stmt_execute($st); 
} 

//close connection 
mysqli_close($con); 

?>

Ich habe einen Fehler gefunden, der besagt "Hinweis: Undefinierter Index: allRoundData in ..." Aber es erscheint dort.

+0

echo $ name = $ row ["name"]; versuchen Sie diese – JYoThI

Antwort

1

Sie müssen eine verschachtelte Schleife verwenden, um alle Werte wie diese zu erhalten und die Verwendung von foreach zu verstehen. foreach wird verwendet, um das Array zu durchlaufen, um den Wert bis zu den N-ten Werten zu erhalten.

Foreach reference

foreach ($array["allRoundData"] as $row) 
    { 

     $name = $row["name"]; 
     $time = $row['timeLimitInSeconds']; 
     $points = $row['pointsAddedForCorrectAnswer']; 

     foreach($row['questions'] as $row1) 
     {  
      $question= $row1['questionText']; 

      foreach($row1['answers'] as $row2) 
      { 

       $answer = $row2['answerText']; 

       $isCorrect= $row2['isCorrect']; 

       echo "INSERT INTO data (round_name, time_limit, points_added, question_text, answer_text, isCorrect) VALUES ('". $name."',".$time.",".$points.",'". $question."','". $answer ."','". $isCorrect."') </br>"; 
      } 

     } 


    } 

OUTPUT:

INSERT INTO data (round_name, time_limit, points_added, question_text, answer_text, isCorrect) VALUES ('Animals',20,10,'Lions are carnivores: true or false?','True','1') 

INSERT INTO data (round_name, time_limit, points_added, question_text, answer_text, isCorrect) VALUES ('Animals',20,10,'Lions are carnivores: true or false?','False','') 

INSERT INTO data (round_name, time_limit, points_added, question_text, answer_text, isCorrect) VALUES ('Animals',20,10,'What do frogs eat?','Pizza','') 

INSERT INTO data (round_name, time_limit, points_added, question_text, answer_text, isCorrect) VALUES ('Animals',20,10,'What do frogs eat?','Flies','1') 

INSERT INTO data (round_name, time_limit, points_added, question_text, answer_text, isCorrect) VALUES ('Animals',20,10,'Where do mice live?','In the sea','') 

INSERT INTO data (round_name, time_limit, points_added, question_text, answer_text, isCorrect) VALUES ('Animals',20,10,'Where do mice live?','On the moon','') 

INSERT INTO data (round_name, time_limit, points_added, question_text, answer_text, isCorrect) VALUES ('Animals',20,10,'Where do mice live?','On land','1') 

INSERT INTO data (round_name, time_limit, points_added, question_text, answer_text, isCorrect) VALUES ('Animals',20,10,'Where do mice live?','In a tree','') 
+0

Oh ja Vielen, vielen Dank , es hat funktioniert !! ... XD – Shiro

+0

markieren Sie es mit grünen Haken ist es nützlich für zukünftige Benutzer @Shiro – JYoThI

0

Ihre Schleife läuft falsch, Sie müssen Daten innerhalb der allRoundData Schleife. Also, versuchen Sie bitte diesen Code

foreach ($data["allRoundData"] as $row) { 

    // get the employee details 
    $name = $row["name"]; 
    $time = $row['timeLimitInSeconds']; 
    $points = $row['pointsAddedForCorrectAnswer']; 
    $question = $row['questions']['questionText'];//wrong code 
    $answer = $row['answers']['answerText'];//wrong code 
    $isCorrect = $row['answers']['isCorrect'];//wrong code 

    // execute insert query 
    mysqli_stmt_execute($st); 
} 

Kommentar //wrong code i geschrieben haben, weil diese Arrays sind, und Sie können nicht direkt zugreifen, ohne Index sollten Sie $row['answers'][$index]['answerText'] verwenden, wo $index ist from 0 to count($row['answers'])-1

Wenn Sie statische Ergebnisse brauchen Verwenden Sie dann einen bestimmten Index, andernfalls müssen Sie diese Variablen loopen, um das gewünschte Ergebnis zu erhalten.

+0

Okey Danke Ich habe das Konzept jetzt ... Dank der Antwort ...: D – Shiro

+0

Vergessen Sie nicht zu upvote, wenn Sie nützliche Antworten für Ihre Fragen finden :) –

0

Du hast in der Komplexität der JSON-Daten verloren haben:

  • Ihre JSON stellt ein Array mit einem Schlüssel : $data['allRoundData'].
  • wiederum hat ein Element $data['allRoundData'][0]
  • In diesem Array, haben Sie $data['allRoundData'][name], $data['allRoundData'][timeLimitInSeconds], $data['allRoundData'][pointsAddedForCorrectAnswer]
  • Danach Sie andere Array: $data['allRoundData'][questions]

Sie sollten die vorläufigen Daten erhalten direkt von $data['allRoundData'][0], und dann erhalten Sie die tatsächlichen Fragen mit:

foreach($data['allRoundData'][0][questions] as $question) {}