user3550256 February 2016

php nested array for json output

I run queries on 3 tables and I'm trying to format results into a nested array. It's the inner array thats got me stumped. sizes array should be in the beer array object but its outside of it. Any help appreciated.

JSON

    {
    "brewerId": "41", 
    "brewerName": "Prancing Pony Brewing", 
    "beers": [
        {
            "beerid": "816", 
            "productName": "Prancing Pony Amber Ale", 
            "ibu": "18.00", 
            "abv": "5.00", 
            "style": "Amber Ale"
        }, 
        {
            "beerid": "817", 
            "productName": "Prancing Pony Copper Ale", 
            "ibu": "25.00", 
            "abv": "5.80", 
            "style": "Indian Pale Ale"
        }, 
        {
            "beerid": "837", 
            "productName": "Prancing Pony Pale Ale", 
            "ibu": "37.00", 
            "abv": "5.50",  
            "style": "Pale Ale"
        }, 
        {
            "beerid": "838", 
            "productName": "Prancing Pony India Red Ale", 
            "ibu": "60.00", 
            "abv": "7.90", 
            "style": "Indian Pale Ale"
        }
    ], 
    "sizes": [
        {
            "beerId": "816", 
            "size": "330ml Bottle"
        }, 
        {
            "beerId": "816", 
            "size": "330ml Can"
        }, 
        {
            "beerId": "837", 
            "size": "345ml Can"
        }, 
        {
            "beerId": "837", 
            "size": "375ml Bottle"
        }
    ]
}, 

PHP

    $result = $func->getBrewers(); 
    $json_response = array(); 
    while ($row = mysqli_fetch_array($result))
    {
        $row_array = array();
        $row_array['brewerId'] = $row['brewerId'];        
        $row_array['brewerName'] = $row['brewerName'];
        $row_array['beers'] = array();
        $brewer_pk = $row['brewerId'];  

        $beer_qry = $func->getBeers($brewer_pk);
        while ($beer_fet = mysqli_fetch_array($beer_        

Answers


Rogier Spieker February 2016

It seems you misindented at some point and just went wrong from that point on.

$result = $func->getBrewers(); 
$json_response = array(); 
while ($row = mysqli_fetch_array($result))
{
    $row_array = array(
        'brewerId'   => $row['brewerId'],
        'brewerName' => $row['brewerName'],
        'beers'      => array()
    );

    $brewer_pk = $row['brewerId'];  
    $beer_qry = $func->getBeers($brewer_pk);
    while ($beer_fet = mysqli_fetch_array($beer_qry))
    {
        $tmp = array(
            'beerid'      => $beer_fet['beerid'],
            'productName' => $beer_fet['productName'],
            'ibu'         => $beer_fet['ibu'],
            'abc'         => $beer_fet['abv'],
            'notes'       => $beer_fet['notes'],
            'style'       => $beer_fet['style'],
            'sizes'       => array()
        );

        $beer_pk = $tmp['beerid'];
        $size_qry = $func->getSizes($beer_pk);
        while ($size_fet = mysqli_fetch_array($size_qry))
        {
            $tmp['sizes'][] = array(
                'beerId' => $size_fet['beerId'],
                'size'   => $size_fet['size'],
            );
        }

        $row_array['beers'][] => $tmp;
    }

    array_push($json_response, $row_array);
}

echo json_encode($json_response);

In order to simplify the referencing somewhat, I've rewritten the beers to be created in a $tmp variable, which now also has a 'sizes' array to which the various sizes will be appended. After the beers and sizes are added, the $tmp will be added to the beers.

Do note that you will now be performing some more database queries, as it now (as requested) fetches the sizes per beer and not just for the last one.

Post Status

Asked in February 2016
Viewed 3,298 times
Voted 9
Answered 1 times

Search




Leave an answer