-1

I develop php code like this :

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;

The result json is :

[
    {
        "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"
            }
        }
    }
]

But I want to combine the same catalog like this :

[
    {
        "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"
                }
            }
        }
    }
]

How can I do that ?

6
  • i dont't think that is the idea with json... you would have 2 times the key category or later multiple times, should be against specification the way you want it Commented Feb 14, 2013 at 10:24
  • Are you saying you just want to overwrite the category element of the $json array? This is rather confusing. Commented Feb 14, 2013 at 10:24
  • I edit my question category to category, category 2 . Can I create that? Commented Feb 14, 2013 at 10:26
  • You have to create your array in that format. Commented Feb 14, 2013 at 10:29
  • What have you done to come up with an answer yourself? The community wants to help you, but we don't like doing your homework. Commented Feb 14, 2013 at 10:34

1 Answer 1

1

You can index the $catalogue by catalogue_id and append category as they come

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;

Instead of array2json you could also use json_encode

echo json_encode(array_values($json));
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help . Catalog are combine but category is empty array.
I got it . I add $json[$catalogue_id] = $catalogue; after array_push($catalogue['categories'], $category); this.
@thinzar Sorry, I missed that. I simplified the code a bit. Please see updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.