如何从JSON对象中获取多个值

时间:2015-10-21 13:49:38

标签: php android

我尝试从我的服务器获取结果。我的应用程序发送一个包含用户信息的数组,并将其插入数据库。

我得到以下结果:

{"flag":"failed","msg":"insert event","event id":"89","invitedusers":"[0508690186, 0508690187]","size_invited":1,"user_query":"SELECT id From users WHERE "}

我想知道如何在“邀请用户”下阅读PHP中的值:“[0508690186,0508690187]”

这是我的PHP代码:

<?php

/ **  *由PhpStorm创建。  *用户:matant  *日期:2015年9月17日  *时间:下午2:56  * / 包括'response_process.php';

类CreateEvent实现ResponseProcess {

 public function dataProcess($dblink)
    {
        $output = array();
        $sport = $_POST["sport_type"];
        $date = date("Y-m-d",strtotime(str_replace('/','-',$_POST["date"])));
        $s_time =$_POST["s_time"];
        $e_time = $_POST["e_time"];
        $lon = $_POST["lon"];
        $lat = $_POST["lat"];
        $event_type = $_POST["event_type"];
        $max_p = $_POST["max_participants"];
        $sched = $_POST["scheduled"];
        $gen = $_POST["gender"];
        $min_age = $_POST["minAge"];

        $query = "SELECT * FROM event WHERE (event.longtitude = '$lon' AND event.latitude = '$lat')
                AND event.event_date = '$date' And ((event.start_time BETWEEN '$s_time' AND '$e_time') OR (event.end_time BETWEEN '$s_time' AND '$e_time'))";

        //AND (event.start_time = '$s_time' AND event.end_time = '$e_time')


        //check time and place of the event
        $result_q = mysqli_query($dblink,$query) or die (mysqli_error($dblink));
        if(!$result_q)
        {
            $output["flag"]= "select failed";
            $output["msg"] = $result_q;
            return json_encode($output);
        }
        //case date and time are available
        else {
            $no_of_rows = mysqli_num_rows($result_q);
            if ($no_of_rows < 1) {
                $output["flag"] = "success";
                $output["msg"] = "insert event";

                $result = mysqli_query($dblink, "INSERT into event(kind_of_sport,event_date,start_time,end_time,longtitude,latitude,private,gender,min_age,max_participants,scheduled,event_status)
             VALUES ('$sport','$date','$s_time','$e_time','$lon','$lat','$event_type','$gen','$min_age','$max_p','$sched','1')") or die (mysqli_error($dblink));
                if (!$result) {
                    $output["flag"] = "failed to create event";
                    // return (json_encode($output));
                }
                if(isset($_POST["invitedUsers"])){
                    $query_id = "SELECT id From event WHERE event.event_date = '$date' and event.start_time = '$s_time' and event.end_time = '$e_time'";
                    $event_s_res = mysqli_query($dblink,$query_id) or die (mysqli_error($dblink));
                    if(!$event_s_res)
                    {
                        $output["flag"] = "failed";
                        $output["msg"] = "Event id not found";
                    }
                    else{
                        $row = mysqli_fetch_assoc($event_s_res);
                        $output["event id"]=$row["id"];
                        $json = json_decode($_POST["invitedUsers"]);
                        $invited_users = str_replace("\\","",$json);

                        $output["invitedusers"] = $_POST["invitedUsers"] ;
                        $output["size_invited"] = count($_POST["invitedUsers"]);
                        $query_users = "SELECT id From users WHERE ";
                        $i=0;
                        foreach($invited_users as $user) {
                            if ($i < (count($invited_users) - 1))
                                // add a space at end of this string
                                $query_users .= "users.mobile = '".$user[$i]."' or ";
                            else {
                                // and this one too
                                $query_users .= "users.mobile = '".$user[$i]."' ";
                                $output["users"][] = $user['mobile'];
                            }
                            $i++;
                            $output["index"]=$i;
                        }
                        $output["user_query"]= $query_users;

                       /* $event_user_s_res = mysqli_query($dblink,$query_users) or die (mysqli_error($dblink));
                        if(!$event_user_s_res)
                        {
                            $output["flag"] = "failed";
                            $output["msg"] = "user id not found";
                        }*/
                    }
                    $output["flag"] = "failed";
                }

            }
            else {

                $output["flag"] = "failed";
                $output["msg"] = "Place is already occupied in this time";
            }
        }
        return json_encode($output);
    }
}

1 个答案:

答案 0 :(得分:0)

我通过从应用程序传递JSON对象并使用来解决此问题 json_decode方法将其转换回来。