数组中的Php数组我无法创建

时间:2017-02-06 21:20:36

标签: php arrays

我想在数组中获取两个数组值。我有两种情况,一种是完成的,另一种是没有完成的。在数组中,我能够获得单个数组,但无法实现倍数,如下面的预期输出中所述。

[{"id":"3","category":"Name","text":"text with some data","image":"http:\/\/examole.net\/folder\/images\/category1.jpg"},{"id":"4","category":"Name","text":"text with some data","image":"http:\/\/examole.net\/folder\/images\/category1.jpg"}]

完成和未完成查询的代码

$response = array();
$response2 = array();
if(isset($_REQUEST['action']) && $_REQUEST['action']=='test') {
    $query="select * from exampledb where id='".$db->escape($_REQUEST['id'])."' and workdone='done'";
    $num= $db->num_rows($query);          
    if($num) {
        $rows = $db->get_results($query);

        foreach($rows as $k=>$row1) { 
            $response[$k]['id'] = $row1['id'];
                $response[$k]['category'] = $row1['category'];
                $response[$k]['text'] = $row1['text'];
                $response[$k]['image'] = $row1['image'];
            }       
        } else{
            $response['message']='No user Found';
            $response['status']='fail';
        }

        $query="select * from exampledb where id='".$db->escape($_REQUEST['id'])."' and workdone='notdone'";
        $num= $db->num_rows($query);          
        if($num) {
            $rows = $db->get_results($query);
            foreach($rows as $k=>$row1) { 
                $response[$k]['id'] = $row1['id'];
                $response[$k]['category'] = $row1['category'];
                $response[$k]['text'] = $row1['text'];
                $response[$k]['image'] = $row1['image'];
            }       
        } else {
            $response['message']='No user Found';
            $response['status']='fail';
        }
        echo json_encode($response);
        echo json_encode($response2);
    }
}

期待输出

    [{Done:{{"id":"3","category":"Name","text":"text with some data","image":"http:\/\/examole.net\/folder\/images\/category1.jpg"},{"id":"4","category":"Name","text":"text with some data","image":"http:\/\/examole.net\/folder\/images\/category1.jpg"} }, 
    Notdone:{{"id":"3","category":"Name","text":"text with some data","image":"http:\/\/examole.net\/folder\/images\/category1.jpg"},{"id":"4","category":"Name","text":"text with some data","image":"http:\/\/examole.net\/folder\/images\/category1.jpg"}    }}]

1 个答案:

答案 0 :(得分:0)

你可以用两种方式做到这一点

<强> 1

保持原样,并在sql / array处理代码之后移动echo

//... [...] ...
}
        else{
            $response['message']='No user Found';
            $response['status']='fail';
        }

 echo json_encode($response); //<---- here for the first

$query="select * from exampledb where id='".$db->escape($_REQUEST['id'])."' and workdone='notdone'";
        $num= $db->num_rows($query);  //.......  

//... [...] ...
}
        else{
            $response['message']='No user Found';
            $response['status']='fail'; 
        }
    echo json_encode($response); //<---- here for the second
//.......  

<强> 2

在您的SECOND查询后更改foearch中的相对变量

// this remains the same from here
 $query="select * from exampledb where id='".$db->escape($_REQUEST['id'])."' and workdone='done'";
        $num= $db->num_rows($query);          
        if($num)
        {
            $rows = $db->get_results($query);

            foreach($rows as $k=>$row1) { 

                $response[$k]['id'] = $row1['id'];
                $response[$k]['category'] = $row1['category'];
                $response[$k]['text'] = $row1['text'];
                $response[$k]['image'] = $row1['image'];
            }       
        }
        else{
            $response['message']='No user Found';
            $response['status']='fail';
        }
// ----------------- to here

// and here ....
        $query="select * from exampledb where id='".$db->escape($_REQUEST['id'])."' and workdone='notdone'";
        $num= $db->num_rows($query);          
        if($num)
        {
            $rows = $db->get_results($query);

            foreach($rows as $k=>$row1) { 
                //... change this with $response2
                $response2[$k]['id'] = $row1['id'];
                $response2[$k]['category'] = $row1['category'];
                $response2[$k]['text'] = $row1['text'];
                $response2[$k]['image'] = $row1['image'];
            }       
        }
        else{
            //... and this
            $response2['message']='No user Found';
            $response2['status']='fail';
        }
// 
echo json_encode($response);
echo json_encode($response2);

如果你没有填充$response2,那么json_encode

就永远不会结束