Jump to content

Php - Creating json arrays.

SoftwareNinja
Go to solution Solved by C2dan88,

Add second entry to $root array like so

$root['Testing02'] = array(
    "name" => "Mike",
    "number" => null,
    "address" => array(
        "Line01" => "321 fake street",
        "Line02" => "Dublin",
        "Line03" => "2",
        "Country" => "Ireland"
    )
);

When calling json_encode, the output be

{
    "Testing01": {
        "name": "Scott",
        "number": null,
        "address": {
            "Line01": "123 fake street",
            "Line02": "Dublin",
            "Line03": "4",
            "Country": "Ireland"
        }
    },
    "Testing02": {
        "name": "Mike",
        "number": null,
        "address": {
            "Line01": "321 fake street",
            "Line02": "Dublin",
            "Line03": "2",
            "Country": "Ireland"
        }
    }
}

 

Hi all,

 

I am working on pulling data down from my server and have ran into a little issue and cant wrap my head around it.

I have this script that generates a json output neatly but I want to expand it for when pulling multiple records down from my database.

 

Right now the output looks like this:

{
    "Testing01": {
        "name": "Scott",
        "number": null,
        "address": {
            "Line01": "123 fake street",
            "Line02": "Dublin",
            "Line03": "4",
            "Country": "Ireland"
        }
    }
}

But what I want to do is create a loop that will out put records like this:

{
    "Testing01": {
        "name": "Scott",
        "number": null,
        "address": {
            "Line01": "123 fake street",
            "Line02": "Dublin",
            "Line03": "4",
            "Country": "Ireland"
        }
    },
    "Testing02": {
        "name": "Mike",
        "number": null,
        "address": {
            "Line01": "321 fake street",
            "Line02": "Dublin",
            "Line03": "2",
            "Country": "Ireland"
        }
    }
}

 

If anyone at all can help me to create a method of printing nicely formatted json like the second example can you please share.

 

Here is my code for the single output:

<?php

header('Content-Type: application/json');

$root = array(
    "Testing01" => array(
        "name" => "Scott",
        "number" => null,
        "address" => array(
            "Line01" => "123 fake street",
            "Line02" => "Dublin",
            "Line03" => "4",
            "Country" => "Ireland"
        )
        )
);

echo json_encode($root, JSON_PRETTY_PRINT);

?>

 

Thanks in advance.

Link to comment
Share on other sites

Link to post
Share on other sites

Add second entry to $root array like so

$root['Testing02'] = array(
    "name" => "Mike",
    "number" => null,
    "address" => array(
        "Line01" => "321 fake street",
        "Line02" => "Dublin",
        "Line03" => "2",
        "Country" => "Ireland"
    )
);

When calling json_encode, the output be

{
    "Testing01": {
        "name": "Scott",
        "number": null,
        "address": {
            "Line01": "123 fake street",
            "Line02": "Dublin",
            "Line03": "4",
            "Country": "Ireland"
        }
    },
    "Testing02": {
        "name": "Mike",
        "number": null,
        "address": {
            "Line01": "321 fake street",
            "Line02": "Dublin",
            "Line03": "2",
            "Country": "Ireland"
        }
    }
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, C2dan88 said:

Add second entry to $root array like so


$root['Testing02'] = array(
    "name" => "Mike",
    "number" => null,
    "address" => array(
        "Line01" => "321 fake street",
        "Line02" => "Dublin",
        "Line03" => "2",
        "Country" => "Ireland"
    )
);

When calling json_encode, the output be


{
    "Testing01": {
        "name": "Scott",
        "number": null,
        "address": {
            "Line01": "123 fake street",
            "Line02": "Dublin",
            "Line03": "4",
            "Country": "Ireland"
        }
    },
    "Testing02": {
        "name": "Mike",
        "number": null,
        "address": {
            "Line01": "321 fake street",
            "Line02": "Dublin",
            "Line03": "2",
            "Country": "Ireland"
        }
    }
}

 

Ahh perfect! Thanks a million :)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×