0

I want to get an array like this:

{
    "version": "1397992135_1932",
    "list": {
        "20514072":["John Carter","FiExJGIpsek.jpg",1],
        "7247045":["Joe Satriani","KzvE54Z4rlA.jpg",0],
        "91120813":["Mikel Arteta","JnPwkLKGeCA.jpg",1]
    }
}

In database I have simple rows:

id | name | photo | vip

How can I format result from query to what I want?

Update

I don't truly understand how to code this to fetch the data from DB if I have 100 records.

1
  • you should post your solution as answer and accept it Commented Apr 24, 2014 at 6:31

2 Answers 2

1

This would be the array setup required to achieve the JSON string you are looking for:

$data = array(
        "version" => "1397992135_1932",
        "list" =>
    array(
        "20514072" => array(
        "John Carter",
        "iExJGIpsek.jpg",
         "1"
         ),
        "7247045" => array(
        "Joe Satriani",
        "KzvE54Z4rlA.jpg",
        "0"
        ),
        "91120813" => array(
        "Mikel Arteta",
        "JnPwkLKGeCA.jpg",
        "1"
    ))
);

echo "<pre>";   
echo json_encode($data);
echo "</pre>";
?>

OUTPUTS:

{
    "version":"1397992135_1932",
    "list":{
        "20514072":["John Carter","iExJGIpsek.jpg","1"],
        "7247045":["Joe Satriani","KzvE54Z4rlA.jpg","0"],
        "91120813":["Mikel Arteta","JnPwkLKGeCA.jpg","1"]
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, sure. My fault. My answer is not correct. How can I get it through PHP (fetch..?) if I have 100 records?
I see you want the PHP code to fetch the data and than restructure the array for the format I posted in my answer? Your question was not clear on that part.
Yes, you're right! And my question was not clear, right again(
1

OK, I found the solution for dynamic array =)

$list = array();
while ($row = $result->fetch_array()) {
    $list[$row['id']] = array($name,$photo,$vip);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.