2017-02-25 12 views
-1

Ich habe ein JSON-Array mit idtypeanswer Ich verschmelzenden id, type und answer zusammen und steckte sie in den id2 Spalte, also warum kann ich nicht bekommen $ Antworten Wert in der Ausgabe ?Parsing json Array und Zusammenführen von Objekten über php

[{"id":"38","answer":[{"option":"3","text":"HIGH"}],"type":"a"}, 
{"id":"39","answer":[{"option":"3","text":"LOW"}],"type":"b"}, 
{"id":"40","answer":["Hello Word"],"type":"c"}] 

Dies ist mein Code:

<?php 
$con=mysqli_connect("localhost","root","","arrayok"); 
mysqli_set_charset($con,"utf8"); 

// Check connection 
if (mysqli_connect_errno()){ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

$sql="SELECT `survey_answers`,us_id FROM `user_survey_start`"; 
if ($result=mysqli_query($con,$sql)){ 
    while ($row = mysqli_fetch_row($result)){ 
     $json = $row[0]; 
     if(!is_null($json)){       

     $json = preg_replace("!\r?\n!", "", $json); 
     $jason_array = json_decode($json,true); 

    // id2 
      $id = array(); 
      foreach ($jason_array as $data) { 
      if (array_key_exists('id', $data)) { 
      if (array_key_exists('type', $data)) { 
      if (array_key_exists('answer', $data)) { 
       foreach($data['answer'] as $ans){ 
       $answers[] = isset($ans['text']) ? $ans['text'] : $ans; 
       } 

      $id[] = ' ID='.$data['id'].', TYPE='.$data['type'].', AWNSER='.$answers; 
      } 
      } 
      } 
      } 
      // lets check first your $types variable has value or not? 
      $ids= implode(',',$id); /// implode yes if you got values 
      $sql1="update user_survey_start set id2='$ids' where us_id=".$row[1];//run update sql 
      echo $sql1."<br>"; 
      mysqli_query($con,$sql1); 

     } 
    } 
} 
mysqli_close($con); 
?> 

Und das ist meine Ausgabe:

update user_survey_start set id2=' ID=38, TYPE=a, AWNSER=Array, 

und ich bekam Notice: Array to string conversion in C:\wamp64\www\json\awnser.php on line 29
ich Wert von $ haben, wollen Antworten

+0

nichts geändert. Und noch habe ich 'update user_survey_start gesetzt id2 =' ID = 38, TYPE = a, AWNSER = Array, ' –

Antwort

0

Gelöst von mir Weil ich nicht awnser Direkt zu id[] setzen könnte, ich Taked awnser wie folgt aus:

$id[] = ' ID='.$data['id'].', TYPE='.$data['type']; 
$id[] = isset($ans['text']) ? ' AWNSER='.$ans['text'] : ' AWNSER='.$ans; 

und dies ist mein Code:

<?php 
$con=mysqli_connect("localhost","root","","arrayok"); 
mysqli_set_charset($con,"utf8"); 

// Check connection 
if (mysqli_connect_errno()){ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

$sql="SELECT `survey_answers`,us_id FROM `user_survey_start`"; 
if ($result=mysqli_query($con,$sql)){ 
    while ($row = mysqli_fetch_row($result)){ 
     $json = $row[0]; 
     if(!is_null($json)){       

     $json = preg_replace("!\r?\n!", "", $json); 
     $jason_array = json_decode($json,true); 

    // id2 
      $id = array(); 
      foreach ($jason_array as $data) { 
      if (array_key_exists('id', $data)) { 
      if (array_key_exists('type', $data)) {  
      if (array_key_exists('answer', $data)) { 
       foreach($data['answer'] as $ans){ 
       $id[] = ' ID='.$data['id'].', TYPE='.$data['type']; 
       $id[] = isset($ans['text']) ? ' AWNSER='.$ans['text'] : ' AWNSER='.$ans; 
       } 



      } 
      } 
      } 
      } 
      // lets check first your $types variable has value or not? 
      $ids= implode(',',$id); /// implode yes if you got values 
      $sql1="update user_survey_start set id2='$ids' where us_id=".$row[1];//run update sql 
      echo $sql1."<br>"; 
      mysqli_query($con,$sql1); 

     } 
    } 
} 
mysqli_close($con); 
?> 
+0

Es wäre toll, wenn Sie ein wenig mehr erklären könnten, wie Sie Ihr Problem gelöst haben :) – user3284463

+0

Ich habe mein Awnser aktualisiert, Weil ich Awnser nicht direkt auf ID [] nach 'ID' und' Typ' setzen konnte, habe ich Awnser Invidued, wie folgt: '$ id [] = 'ID ='. $ Data ['id']. ', TYPE = '. $ Data [' Typ ']; $ id [] = isset ($ ans ['text'])? 'AWNSER ='. $ Ans ['Text']: 'AWNSER ='. $ Ans; ' –