通过php解析json数组和合并对象

时间:2017-02-25 11:47:45

标签: php mysql arrays json

我有一个带有onMessage importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-app.js'); importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js'); firebase.initializeApp({ 'messagingSenderId': 'YOUR-SENDER-ID' }); const messaging = firebase.messaging(); messaging.setBackgroundMessageHandler(function(payload) { console.log('[firebase-messaging-sw.js] Received background message ', payload); const notificationTitle = 'Background Message from html'; const notificationOptions = { body: 'Background Message body.', icon: '/firebase-logo.png' }; return self.registration.showNotification(notificationTitle, notificationOptions); }); id的json数组我正在将typeanswerid合并在一起并将它们放入type列,为什么我不能在输出中获得$ answers值?

answer

这是我的代码:

id2

这是我的输出:

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

我得到<?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); ?>
我想拥有$ answers的价值

1 个答案:

答案 0 :(得分:0)

由我自己解决 因为我无法将Awnser直接放到id[],所以我将Awnser打成了这样:

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

这是我的代码:

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