PHP数组无法使用JSON正确格式化

时间:2017-11-20 15:09:18

标签: php arrays

我已经生成了一个函数,它将遍历我的数据库表中的记录并将它们转换为数组以供进一步使用

public function cycleCount()
{
    global $db;

    $selectProducts = $db->query("SELECT * FROM stockroom_products");

    $results = array("catA", "catB", "catC");

    while($row = $db->assoc($selectProducts))
    {
        if($row['Product_PRICE'] >= 10)
        {
            array_push($results, "catA", $row['ID']);
        }
        else if ($row['Product_PRICE'] >= 5 AND $row['Product_PRICE'] < 10) 
        {
            array_push($results, "catB", $row['ID']);
        }
        else if ($row['Product_PRICE'] > 0 AND $row['Product_PRICE'] < 5)
        {
            array_push($results, "catC", $row['ID']);
        }


        $json_data = json_encode($results);
        //echo $json_data;

        echo $json_data;

    }

}

我似乎无法弄清楚如何使数组像

[catC[ID1, ID2, ID3]], [catB[ID1, ID2, ID3]], [catA[ID1, ID2, ID3]]

目前这样做

["catA","catB","catC","catB","1"]["catA","catB","catC","catB","1","catC","2"]["catA","catB","catC","catB","1","catC","2","catC","3"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10","catC","11"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10","catC","11","catC","12"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10","catC","11","catC","12","catC","13"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10","catC","11","catC","12","catC","13","catA","14"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10","catC","11","catC","12","catC","13","catA","14","catA","16"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10","catC","11","catC","12","catC","13","catA","14","catA","16","catB","17"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10","catC","11","catC","12","catC","13","catA","14","catA","16","catB","17","catA","18"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10","catC","11","catC","12","catC","13","catA","14","catA","16","catB","17","catA","18","catA","19"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10","catC","11","catC","12","catC","13","catA","14","catA","16","catB","17","catA","18","catA","19","catB","20"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10","catC","11","catC","12","catC","13","catA","14","catA","16","catB","17","catA","18","catA","19","catB","20","catB","21"]

3 个答案:

答案 0 :(得分:2)

您正在将值推送到不起作用的字符串。

尝试定义结果数组,如下所示: $result = array("catA" => array(), "catB" => array(), "catC" => array());

这应该可以让您获得嵌套数组,从而获得您所寻求的结果。 也可以使用array_push($result['catA'], $row['ID']);代替array_push($reuslt, 'catA', $row[ID'])

正如@u_mulder正确地说明json应该在while循环后回显!

答案 1 :(得分:0)

array_push根据文档http://php.net/manual/de/function.array-push.php

不作为第二个参数键入

你应该这样做:

$results = array("catA" => [], "catB" => [], "catC" => []);
if($row['Product_PRICE'] >= 10)
{
    $results["catA"][] = $row['ID'];
}
else if ($row['Product_PRICE'] >= 5 AND $row['Product_PRICE'] < 10) 
{
    $results["catB"][] = $row['ID'];
}
else if ($row['Product_PRICE'] > 0 AND $row['Product_PRICE'] < 5)
{
    $results["catC"][] = $row['ID'];
}

这对你有用吗?

答案 2 :(得分:0)

试试吧

      piblic  function cycleCount(){
    global $db;

    $selectProducts = $db->query("SELECT * FROM stockroom_products");
    $catA =array();
    $catB =array();
    $catC =array();
    $results = array();

    while($row = $db->assoc($selectProducts))
    {
        if($row['Product_PRICE'] >= 10)
        {
             array_push($catA,array('catA' =>$row['ID']));
        }
        else if ($row['Product_PRICE'] >= 5 AND $row['Product_PRICE'] < 10) 
        {
            array_push($catB,array('catB' =>$row['ID']));

        }
        else if ($row['Product_PRICE'] > 0 AND $row['Product_PRICE'] < 5)
        {

            array_push($catC,array('catC' =>$row['ID']));

        }


        $json_data = json_encode($results);
        //echo $json_data;

        echo $json_data;

    }
    $results=array($catA,$catB,$catC);
    print_r($results);
}
?>