【问题标题】:How to place the json objects into array usin php?如何使用 php 将 json 对象放入数组中?
【发布时间】:2015-02-05 06:55:59
【问题描述】:

这是我的php代码,获取json格式数据

if($status==1)
    {
        $post_id=$json_object['post_id'];

        $get_postid=mysqli_query($con,"select * from User_Post where post_id='$post_id'");
        if(mysqli_num_rows($get_postid)==0)
        {

            //For failure status if session id is wrong.
             http_response_code(500);
             echo json_encode(array("error_code"=>"500","error_message"=>"Sorry, post id does not exists."));
        }
        else
        {
        foreach($field_check as $values)
        {
            if($values=='id')
            {
                $row_array = array();
                while ($row = $get_postid->fetch_array())
                {
                    $row_array['id']=$row['post_id'];
                    $row_array['image_urls']=explode(',',$row['post_image_url']);
                    $storetag= explode(',',$row['Post_tagged_id']);   
                    $has_liked="false";
                    $has_commented="false";

                }

            }
            elseif($values=='tagged_users')
            {

                while ($row = $get_postid->fetch_array())
                {
                    $storetag= explode(',',$row['Post_tagged_id']);
                    $has_liked="false";
                    $has_commented="false";
                }
                for($i=0;$i<count($storetag);$i++)
                {

                    $user=mysqli_query($con,"select user_id, profile_image_url from Wheel_User where user_id='$storetag[$i]'");

                    if(mysqli_num_rows($user)==0)
                    {
                        //For failure status if session id is wrong.
                        http_response_code(500);
                        echo json_encode(array("error_code"=>"500","error_message"=>"Sorry, post id does not exists.".die()));
                    }

                    else
                    {

                        while ($row = $user->fetch_array())
                        {

                            $tagged_users[$i]['user_id']=$row['user_id'];
                            $array['user_id']=$tagged_users[$i]['user_id'];
                            $pro_image_url[$i]=$row['profile_image_url'];
                            $short_image_url[$i]=str_replace('_b','_t',$pro_image_url[$i]);
                            $short_image_url[$i]=str_replace('/images/','/thumbnails/',$short_image_url[$i]);
                            $array['short_image_url']=$short_image_url[$i];

                        }

                    }

                    array_push($row_array,$array);

                }

            }

        }

            array_push($json_response,$row_array);
            echo str_replace('\/','/',json_encode($json_response)); 
    }
}

它正在返回以下对象

[
{
"id": "1111",
"image_urls": [
  "https://docs.google.com/document/d/14kVqw9d2kzYIEClN-SVp2Co2mlglM9F-8HIy0ggTZ3g/edit",
  "http://stackoverflow.com/questions/21762150/how-do-i-insert-data-from-a-json-array-into-mysql-database",
  "https://drive.google.com/?authuser=0#my-drive",
  "https://drive.google.com/?tab=mo&authuser=0#shared-with-me"
 ],

"0": {
  "user_id": "111",
  "short_image_url": "chrome://restclient/content/thumbnails/restclient_t.jpg"
},
"1": {
  "user_id": "321",
  "short_image_url": "chrome://restclient/thumbnails/restclient_t.jpg"
},
"2": {
  "user_id": "1234",
  "short_image_url": "http://54.169.40.195/wheel/wheel/service/testing/chetan/audio/c71dfe45421b2864476a0bde257f0a57e72084783ce859e26595599670904907.mp3"
 }
 }
]

但我想像这样为该数组分配名称

[
  {
  "id": "1111",
  "image_urls": [
  "https://docs.google.com/document/d/14kVqw9d2kzYIEClN-SVp2Co2mlglM9F-8HIy0ggTZ3g/edit",
  "http://stackoverflow.com/questions/21762150/how-do-i-insert-data-from-a-json-array-into-mysql-database",
  "https://drive.google.com/?authuser=0#my-drive",
  "https://drive.google.com/?tab=mo&authuser=0#shared-with-me"
  ],

 "tagged_users":[
"0": {
  "user_id": "111",
  "short_image_url": "chrome://restclient/content/thumbnails/restclient_t.jpg"
},
"1": {
  "user_id": "321",
  "short_image_url": "chrome://restclient/thumbnails/restclient_t.jpg"
},
"2": {
  "user_id": "1234",
  "short_image_url": "http://54.169.40.195/wheel/wheel/service/testing/chetan/audio/c71dfe45421b2864476a0bde257f0a57e72084783ce859e26595599670904907.mp3"
}
]

} 我不知道我必须在哪里添加这个数组名称。请帮我解决这个问题。 我不能像这样分配数组名称,因为 $row_array 还包含其他数据

echo str_replace('\/','/',json_encode(array("tagged_user"=>$json_response));

【问题讨论】:

    标签: php arrays json


    【解决方案1】:

    倒数第二行应该是你想要的:

    array_push($json_response,array("tagged_users" => $row_array));
    

    编辑删除第二部分。

    【讨论】:

    • 现在我上传了我的整个代码,我希望你现在能得到清晰的图片,
    • 它工作得很好,但是 tagged_user 应该是一个数组,即 "tagged_user":[............ .....]
    • 好吧,如果它不这样做,那么它必须在你之前做的数组 push 中,这也应该在你的循环中 array_push($row_array,array( $i => $array)) ;也添加它
    【解决方案2】:

    试试 -

    array_push($row_array['tagged_users'],$array);
    

    或者像这样定义它 -

    $row_array['tagged_users'] = array();
    

    那么所有的值都会如你所愿。

    【讨论】:

    • 我试过了,但它给出了一个错误“array_push() 期望参数 1 是数组,null”
    • 并且 row_array 已经包含了另外两个值,所以如果我将名称分配给行数组,那么它会将名称分配给整个 json 数组。我希望你明白我在说什么。
    • 你在哪里定义$row_array
    • 在代码的开头,当我开始将值放入该数组时。所以它已经有两个值了
    • 现在我上传了我的整个代码,我希望你现在能得到清晰的图片,
    猜你喜欢
    • 2021-08-22
    • 2021-07-18
    • 2020-10-20
    • 2022-07-11
    • 2017-07-27
    • 2022-01-15
    • 2013-05-19
    • 2020-03-30
    • 2017-05-13
    相关资源
    最近更新 更多