1

Looking to output json in the following format:

 [{"Montgomery":[{"red":"12:34:56","yellow":"11:44:46","orange":"10:54:36","green":"9:24:26","purple":"8:14:16"}],"Suburban":[{"red":"12:34:56","yellow":"11:44:46","orange":"10:54:36","green":"9:24:26","purple":"8:14:16"}],"Shady Grove Adventist":[{"red":"12:34:56","yellow":"11:44:46","orange":"10:54:36","green":"9:24:26","purple":"8:14:16"}],"Holy Cross":[{"red":"12:34:56","yellow":"11:44:46","orange":"10:54:36","green":"9:24:26","purple":"8:14:16"}],"Washington Adventist":[{"red":"12:34:56","yellow":"11:44:46","orange":"10:54:36","green":"9:24:26","purple":"8:14:16"}]}]

My code:

 $xyz[] = array("Montgomery"=> array("Red" => "12:00", "Yellow" => "14:00"));
 $xyz[] = array("Suburban"=> array("Yellow" => "16:00"));

 echo '[' . json_encode($xyz) . ']';

My results:

[[{"Montgomery":{"Red":"12:00","Yellow":"14:00"}},{"Suburban":{"Yellow":"16:00"}}]] 
0

3 Answers 3

2

This will give you the structure:

$container = array();
$container['Montgomery'] = array(array('red' => '12:34:56', 'yellow' => '11:44:46'));
$container['Suburban'] = array(array('red' => '12:34:56', 'yellow' => '11:44:46'));
echo json_encode(array($container));
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, that was exactly what I was looking for... @wwwslinger: I wanted to keep it all in an array and not create a class, but excellent options, thank you both.
1

You could use objects, something like this (you'll want to clean it up a bit):

class Church {
  public $red = "";
  public $yellow = "";
  public $orange = "";
  public $green = "";
  public $purple = "";
}
class XYZ {
  public $Montgomery = new Church();
  public $Shad_Grove_Adventist = new Church();
  public $Holy_Cross = new Church();
  public $Washington_Adventist = new Church();
}

$xyz = new XYZ();
$xyz->Montgomery->red = "12:00";
...

then output your JSON:

echo '[' . json_encode($xyz) . ']';

It won't be a perfect match to your desired JSON output, but it will give better readability and much more flexibility.

2 Comments

how is this a valid variable with a space? public $Shad_Grove Adventist ? ;)
Sorry, it needs underscores.
0

You're desired output has an array [] as its top level item. [] in JSON corresponds to a numeric array in PHP. A JSON array is used to contain an ordered collection of items.

Your top-level array contains one item, a JSON object {}. A PHP object (stdClass) or associative array will convert to this in JSON. A JSON object is used to create collections of key-value pairs.

To produce your desired output, build your data in PHP like this:

// Top-level numeric array.
$container = array();

// Only item in top-level array.
$item = array();

// The value of each of these items will be a numeric array.
$item['Montgomery'] = array();

// Create a new associative array, and push that onto the list array.
$subItem = array("red" => "12:34:56",
        "yellow" => "11:44:46",
        "orange" => "10:54:36",
        "green" => "9:24:26",
        "purple" => "8:14:16");
$item['Montgomery'][] = $subItem;
$container[] = $item; 

// ...Add more items...

print json_encode($container);

Or, if you want to build it all at once:

$container = array(
    array(
        "Montgomery" => array(
            array("red" => "12:34:56",
                "yellow" => "11:44:46",
                "orange" => "10:54:36",
                "green" => "9:24:26",
                "purple" => "8:14:16"
            )
        )
        // ...More items...
    )
);

print json_encode($container);

Notice that there are places that have an array inside an array. This is to build an associative array and add it as the only member of a numeric array. This corresponds to having a {} inside [].

Comments

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.