-1

我开发这样的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"
                }
            }
        }
    }
]

我怎样才能做到这一点 ?

4

1 回答 1

1

您可以索引$catalogueby并在它们出现时catalogue_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));
于 2013-02-14T11:36:16.977 回答