1

I have got a simple function which outputs an array which i can then just "json_encode" This works fine. But now i need to do this multiple times.

while($row = mysql_fetch_assoc($resultsol)) {
                    $all[] = jsonoutput("$row[appid]");
}

But when i json_encode($all) this, it will have a first "header" sort of speak. Which i dont want.

What i get now (showing a sample part)

[
  - {
      - Firstentry: {
    info1: "bla",
    info2: "bla2",
        cell {
            color: "green",

But what i want is:

{
      - Firstentry: {
    info1: "bla",
    info2: "bla2",
        cell {
            color: "green",

I see 2 possible sollutions, the json_encode should start 1 level deeper/lower (always mix those up) or the first array should be removed before the json_encode.

Either way, i am lost in between the 2. Any help would be appreciated.

UPDATE: added array output:

Good:

object(stdClass)#1 (1) { ["Firstentry"]=> object(stdClass)#2 (11) { ["info1"]=> string(3) "bla" ["info2"]=> string(3) "bla2"

Wrong:

array(63) { [0]=> object(stdClass)#1 (1) { ["Firstentry"]=> object(stdClass)#2 (10) { ["info1"]=> string(3) "bla" ["info2"]=> string(3) "bla2" 

p.s. the function is called above jsonoutput, but this just outputs an array, i json_encode it in the end.

2
  • json_encode should only be called once. Create your array, then json_encode the entire thing once at the end. Commented Jun 21, 2013 at 14:36
  • as in the update above, its only a function that i call. I do json_encode "after" the loop has filled the array. Commented Jun 21, 2013 at 20:25

1 Answer 1

1

I resolved this by changing it into:

            while($row = mysql_fetch_array($resultsol)) {
                    $output = jsonoutput("$row[appid]");
                    $all = array_merge($all, (array) $output);
            }

Then doing the json_encode.

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

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.