在php json编码中组合数组

时间:2013-02-14 10:20:53

标签: php json

我开发这样的PHP代码:

while($row = mysql_fetch_array ($result))     
    {

        $catalogue = array(
            'catalogue_id' => $row[0],          
            'catalogue_name' => $row[1],
            'catalogue_cover' => $row[2],

            'category' =>  array(
                            'id' => $row[3],
                            'name' => $row[4],
                            'cover' => $row[5],
                            'item' => array (
                                'id' => $row[6],
                                'name' => $row[7],
                                'cover' => $row[8],
                                'description' => $row[9],
                                'price' =>$row[10]
                            )
            ),                  
        );
        array_push($json, $catalogue);              
    }   
    $jsonresult  =  array2json($json);
    echo $jsonresult;

结果json是:

[
    {
        "catalogue_id": "59",
        "catalogue_name": "IT Catalog",
        "catalogue_cover": "http://192.168.0.22:90/Ecatalogue/catalogue/covers/511b21f398969.jpeg",
        "category": {
            "id": "60",
            "name": "Computer Accessory",
            "cover": "http://192.168.0.22:90/Ecatalogue/category/covers/511b2e11e8b26.jpg",
            "item": {
                "id": "61",
                "name": "CD",
                "cover": "http://192.168.0.22:90/Ecatalogue/item/covers/511b2e1da3063.jpg",
                "description": "",
                "price": "0.00"
            }
        }
    },
    {
        "catalogue_id": "59",
        "catalogue_name": "IT Catalog",
        "catalogue_cover": "http://192.168.0.22:90/Ecatalogue/catalogue/covers/511b21f398969.jpeg",
        "category": {
            "id": "61",
            "name": "IT Category",
            "cover": "http://192.168.0.22:90/Ecatalogue/category/covers/511caf7329f63.jpeg",
            "item": {
                "id": "63",
                "name": "IT Item",
                "cover": "http://192.168.0.22:90/Ecatalogue/item/covers/511cafa17cce5.jpeg",
                "description": "",
                "price": "0.00"
            }
        }
    }
]

但我希望将这样的目录结合起来:

[
    {
        "catalogue_id": "59",
        "catalogue_name": "IT Catalog",
        "catalogue_cover": "http://192.168.0.22:90/Ecatalogue/catalogue/covers/511b21f398969.jpeg",
        "category": {
            "id": "60",
            "name": "Computer Accessory",
            "cover": "http://192.168.0.22:90/Ecatalogue/category/covers/511b2e11e8b26.jpg",
            "item": {
                "id": "61",
                "name": "CD",
                "cover": "http://192.168.0.22:90/Ecatalogue/item/covers/511b2e1da3063.jpg",
                "description": "",
                "price": "0.00"
            },
        "category-2": {
                "id": "61",
                "name": "IT Category",
                "cover": "http://192.168.0.22:90/Ecatalogue/category/covers/511caf7329f63.jpeg",
                "item": {
                    "id": "63",
                    "name": "IT Item",
                    "cover": "http://192.168.0.22:90/Ecatalogue/item/covers/511cafa17cce5.jpeg",
                    "description": "",
                    "price": "0.00"
                }
            }
        }
    }
]

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您可以$cataloguecatalogue_id编制索引,并附加category

while ($row = mysql_fetch_array($result)) {
    $catalogue_id = $row[0];
    if (!isset($json[$catalogue_id])) {
        $json[$catalogue_id] = array(
            'catalogue_id' => $catalogue_id,
            'catalogue_name' => $row[1],
            'catalogue_cover' => $row[2]
            'categories' => array();
            );
    }

    $json[$catalogue_id]['categories'][] = array(
        'id' => $row[3],
        'name' => $row[4],
        'cover' => $row[5],
        'item' => array (
            'id' => $row[6],
            'name' => $row[7],
            'cover' => $row[8],
            'description' => $row[9],
            'price' =>$row[10]
        );
}

$jsonresult = array2json(array_values($json));
echo $jsonresult;

您也可以使用array2json

代替json_encode
echo json_encode(array_values($json));