1

I have a php query the returns the following JSON format from a table.

[{"memberid":"18", 
"useridFK":"30",
"loginName":"Johnson",
"name":"Frank",
"age":"23",
"place":"School",
},

It needs the following format:

[{"memberid":"18" {
                    "useridFK":"30",
                    "loginName":"Johnson",
                    "name":"Frank",
                    "age":"23",
                    "place":"School",}
                    },

I was told in another question that PHP would work and it looks like "Transversing" might be appropriate, I'm looking to find out what to put in the Php before it returns the JASON.

My Array.plist will look like the following:

  Root:                   Dictionary
       V Rows:            Array
         V Item 0:        Dictionary
            Title:        String          18
          V Children      Array
            V Item 0      Dictionary
              Title       String          30
   etc.

Thanks in advance.

1
  • The desired format in "It needs the following format:" is invalid. Reexplain. Commented Jun 13, 2010 at 19:04

1 Answer 1

1

I'm not entirely sure I understand what you want to do. I suppose you want to make the json data into an array in php, and then reorgnize its content to spread over two dimentions?

Php comes with two very nifty functions called "json_decode" and "json_encode" which will allow you to decode and encode json data. You can read more about them here.

A note about "json_decode". Unless you pass on the secondary parameter to the function as TRUE, it will return an object, and not a multi-dimentional array.

Example:

<?php

    $strJsonData = '[
        {"memberid":"18", 
        "useridFK":"30",
        "loginName":"Johnson",
        "name":"Frank",
        "age":"23",
        "place":"School"},

        {"memberid":"19", 
        "useridFK":"36",
        "loginName":"Jones",
        "name":"Bill",
        "age":"34",
        "place":"Work"}
    ]';

    $arrRawJsonData = json_decode( $strJsonData, true );
    # Now $arrRawJsonData contains a two-dimentional array of all your json data.

    $intJsonDataCount = count( $arrRawJsonData );

    for ($i = 0; $i < $intJsonDataCount; $i++)
    {
        $intMemberId = (int) ($arrRawJsonData[$i]['memberid']);
        unset( $arrRawJsonData[$i]['memberid'] );
        $arrJsonData[$intMemberId] = $arrRawJsonData[$i];
    }

    print_r( $arrJsonData );

?>

The above code will result in the following:

Array
(
    [18] => Array
        (
            [useridFK] => 30
            [loginName] => Johnson
            [name] => Frank
            [age] => 23
            [place] => School
        )

    [19] => Array
        (
            [useridFK] => 36
            [loginName] => Jones
            [name] => Bill
            [age] => 34
            [place] => Work
        )

)

Hope this helps!

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.