3

So I am trying to make a JSON object that should hold some information about some questions for me. This is a pseudo-code of how I want it to be presented:

{
 "page" : 1,
 "info" :
          {
           "id" : 1,
           "type" : 3,
           "description": "How to do JSON?",
           "alternatives" : 
                           {
                            "id" : 1,
                            "description" : "Dunno"
                           }
                           {
                            "id" : 2,
                            "description" : "Let me show you"
                           }
                           { 
                            "id" : 3,
                            "description" : "I refuse to show you"
                           }
           }
           "id" : 2,
           "type" : 1,
           "description": "Do you get it?",
           "alternatives" : 
                           {
                            "id" : 1,
                            "description" : "Yes"
                           }
                           {
                            "id" : 2,
                            "description" : "No"
                           }
           }
}

So the code underneath is from Nightmare (one of the answers), and it does exactly what I want to do with the page and the questions, but I can't seem to figure out how to connect alternatives to each question. You can see a snippet at the bottom where I have tried but it's not correctly spelled and I've been hammering at this for a while now.

$before_json_encode[$row['page']][] = array(
  'id' => $row['id'],
  'type' => $row['type'],
  'description' => $row['description'],
  'alternatives' => $alternativesarray//im not sure about here,dont know the structure of the alternative array
);

Another illustration on how I want the hierarchy of the JSON data to appear. I need to be able to pick for instance: All alternatives to all questions on a specific page. So if I want to generate page 3 in my poll I can first find the questions within the page 3 sub-array, and then again from each question gain access to all of it's connected alternatives in that questions's own sub-array. Sorry for the poor explanation of my issue, it's just a bit complicated :/

Page
 Question
  Alternative
  Alternative
  Alternative
 Question
  Alternative
  Alternative
  Alternative
 Question
  Alternative
  Alternative
Page
 Question
  Alternative
  Alternative
 Question
  Alternative
  Alternative

Update: 3rd layer:

$rows_test2[$r['page']]
['id' => $r['id'],
'type' => $r['type'],
'description' => $r['description']]
[] =
        array (
        'altid' => $t['altid'],
        'altdesc' => $t['altdesc']);
Tom
  • 1,747
  • 5
  • 23
  • 39
  • You have `$alternativesarray[]`, should this not be `$alternativesarray` or `$alternativesarray[#ID#]` – Adam Heath Jul 12 '12 at 08:13
  • "This does not really work" does not really mean anything. How exactly is it not working? – lanzz Jul 12 '12 at 08:13
  • get the result from database and then json_encode to convert the array to Json format. http://php.net/manual/en/function.json-encode.php – Vimalnath Jul 12 '12 at 08:15
  • 1
    if you can get the array structure right the json_encode function will create correct JSON, but consider the alternatives part would be some like "alternatives":[{1:"Dunnio"},{2:"Let me show you"},{3:"I refuse to show you"}] – Carlo Moretti Jul 12 '12 at 08:16
  • 1
    You have `$row` getting data from the data base and then you use `$r`when creating the new array. – David Casillas Jul 12 '12 at 08:17
  • and you need to store that array at some Index or push(); array into array ,not like you did by saving it at $rows[] – Rosmarine Popcorn Jul 12 '12 at 08:18
  • If you want `$alternativesarray`to be an empty array, the syntax should be `$alternativesarray()`. – David Casillas Jul 12 '12 at 08:18
  • 1
    @Onheiron Thanks for pointing that out, I see now I have written the pseudo code abit weird, will try to make it more clear what I am trying to achieve now. – Tom Jul 12 '12 at 08:23
  • @DavidCasillas I want it to be filled, just taking one step at a time. I want to connect X amount of alternatives to each question, and X amount of questions to each page. I am just having some problems explaining my goal here. I think the first code snippet explains it better now. – Tom Jul 12 '12 at 08:40

2 Answers2

3
$rows[] = array(
    "page" => 1,
    "info" => array(
        "id" => 1,
        "type" => 3,
        "description" => 'desc',
    )
);
echo json_encode($rows); // [{"page":1,"info":{"id":1,"type":3,"description":"desc"}}]

Update:

$alternativesarray[]=array('id'=>'1', 'description'=>'yes');
$alternativesarray[]=array('id'=>'2', 'description'=>'no');
$rows[] = array(
    "page" => 1,
    "info" => array(
        "id" => 2,
        "type" => 3,
        "description" => 'desc',
        "alternatives" => $alternativesarray
    )
); 
print json_encode($rows); // [{"page":1,"info":{"id":2,"type":3,"description":"desc","alternatives":[{"id":"1","description":"yes"},{"id":"2","description":"no"}]}}]
The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • Isn't that the exact same thing I did except with static values? Or am I missing something? – Tom Jul 12 '12 at 08:34
  • It should be same unless you miss something. – The Alpha Jul 12 '12 at 08:35
  • Actually `json_encode` encodes the `php array` in to `json` so if your array is right then it should work. make sure your array is populating properly. – The Alpha Jul 12 '12 at 08:42
  • It's the structure of the arrays I can't really seem to grasp how to build and populate, added some more information to the question that hopefully explains my question better. Sorry for the poor expression initially. – Tom Jul 12 '12 at 08:46
  • Not sure if that will help me, if you see Nightmare's answer - he pretty much nailed it. I just have to add another layer to that to get the alternatives added to each question. trying with $array[$row['page']]['id' => $r['id'],'type' => $r['type'],'description' => $r['description']][] = array ('altid' => $t['altid'],'altdesc' => $t['altdesc']); for each MySQL line. But it's not working, yet. I just get some sort of error message, so guessing some syntax error somewhere. – Tom Jul 12 '12 at 09:39
  • 1
    That was only an example, you have used `"alternatives" => $alternativesarray[]` in a wrong way and I gave you `"alternatives" => $alternativesarray` but used the `$alternativesarray[]=array('id'=>'1', 'description'=>'yes');` only for an example but thanks, anyway. :-) – The Alpha Jul 12 '12 at 09:43
  • http://stackoverflow.com/questions/11504042/encoding-json-from-mysql/11504422#11504422 – Tom Jul 16 '12 at 13:06
1

maybe like this?

$before_json_encode[$row['page']][] = array(
      'id' => $row['id'],
      'type' => $row['type'],
      'description' => $row['description'],
      'alternatives' => $alternativesarray//im not sure about here,dont know the structure of the alternative array
);
Nightmare
  • 251
  • 1
  • 5
  • 'description' = $row['description'] and alternatives consist of 'id','description' and 'answer'. You might be onto something there. – Tom Jul 12 '12 at 08:55
  • You're my saviour! It worked perfectly! Just have to figure out how to populate the $alternatives array the same way I(you) did the questions now. Magnificent! – Tom Jul 12 '12 at 09:01
  • Any idea how to add a 3rd layer (the alternatives), assuming each alternative also had 3 attributes (id,answer,description) that should be hooked to each question like the questions are hooked to each page. Posted my current attempt in the question post as an update at the bottom. – Tom Jul 12 '12 at 09:43