2

This is the JSON output i want

{
   "MainEvent":"Geelong v Essendon",
   "OutcomeDateTime":"2014-06-27 19:51:00.0000000",
   "Competitors":[
      {
         "Name":"Geelong",
         "Win":"1.32"
      },
      {
         "Name":"Essendon",
         "Win":"3.40"
      }
   ]
},
{
  "MainEvent":"Hawthorn v Gold Coast",
  "OutcomeDateTime":"2014-06-28 13:46:00.0000000",
   "Competitors":[
      {
         "Name":"Geedlong",
         "Win":"1.32d"
      },
      {
         "Name":"Essenddon",
         "Win":"3.40d"
      }
   ]
}

This is my code

foreach ($SortedByDate as $key => $values){
    foreach ($json_a as $root_element => $childnode) {
        foreach( $childnode as $cKey => $subChild) {
            $rootObj = array(
                'MainEvent' => $subChild['MainEvent'], 
                'OutcomeDateTime' => $subChild['OutcomeDateTime'], 
                foreach($subChild['Competitors']['Competitors'] as $compKey => $compVal) {
                    $teamName = $compVal['Team'];
                    $win = $compVal['Win'];
                    $abc = array(
                        "Team" => $teamName,
                        "Win" => $win,
                    ); 
                }   
            } 
            $rootObj ['Competitors'] = $abc;  
        }} 
         $abc="";
        print json_encode($rootObj );
}

And i am getting this output . But comma is missing in my output. Can anyone give me some suggestions please?

  {
       "MainEvent":"Geelong v Essendon",
       "OutcomeDateTime":"2014-06-27 19:51:00.0000000",
       "Competitors":[
          {
             "Name":"Geelong",
             "Win":"1.32"
          },
          {
             "Name":"Essendon",
             "Win":"3.40"
          }
       ]
    }
    {
      "MainEvent":"Hawthorn v Gold Coast",
      "OutcomeDateTime":"2014-06-28 13:46:00.0000000",
       "Competitors":[
          {
             "Name":"Geedlong",
             "Win":"1.32d"
          },
          {
             "Name":"Essenddon",
             "Win":"3.40d"
          }
       ]
    }

I have tried these code but i am not sure how to add comma in my output JSON

5
  • 3
    The JSON output you want is not valid JSON. Commented Jun 23, 2014 at 6:38
  • before { which is above MainEvent element can you check the output Commented Jun 23, 2014 at 6:39
  • @MikeW i am missing Root which i am printing using echo ("Sports"); if you can suggest me better way to get this done then i would appreciate your efforts Commented Jun 23, 2014 at 6:40
  • You're replying to answers with other errors. Your code above can't actually be running as you have posted it. See the line 'OutcomeDateTime' => $subChild['OutcomeDateTime'], -- I would think you want it to be 'OutcomeDateTime' => $subChild['OutcomeDateTime']); Something is varying between what you're running and what you're showing. Commented Jun 23, 2014 at 7:08
  • yes i edit my code and i just copied the relevant code from my code structure and i am not replying with any new errors. Commented Jun 23, 2014 at 7:11

2 Answers 2

1

You have to wrap the root objects in another array:

$rootObjects = [];
foreach ($SortedByDate as $key => $values){
    // ...
    $rootObjects[] = $rootObj;
}
echo json_encode($rootObjects);

It will output [{object1}, {object2}, ....], i.e. the output includes two square brackets that weren't present in your expected output but are important to make it valid JSON.

Sign up to request clarification or add additional context in comments.

1 Comment

@user3754380 well your code gives me a parse error =p
0

You're printing it twice, it will not have comma;

Try this:

$jsons = array();
    foreach ($SortedByDate as $key => $values){
        foreach ($json_a as $root_element => $childnode) {
            foreach( $childnode as $cKey => $subChild) {
                $rootObj = array(
                    'MainEvent' => $subChild['MainEvent'], 
                    'OutcomeDateTime' => $subChild['OutcomeDateTime'], 
                    foreach($subChild['Competitors']['Competitors'] as $compKey => $compVal) {
                        $teamName = $compVal['Team'];
                        $win = $compVal['Win'];
                        $abc = array(
                            "Team" => $teamName,
                            "Win" => $win,
                        ); 
                    }   
                } 
                $rootObj ['Competitors'] = $abc;  
            }} 
             $abc="";
             $jsons[] = json_encode($rootObj);
    }
print implode(",",$jsons);

doesn't look like a valid JSON to me though

let me know if it works.

1 Comment

this one is not working i am getting error arse error: syntax error, unexpected 'foreach' (T_FOREACH), expecting ')'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.