0

I am pulling data from my database and trying to encode into JSON data using json_encode. But to make it easier to read in my android app. I was hopping to format it differently then I am currently doing. Please see bottom encode string example. Any help would be great. Thanks in Advance.

$result = $db->query($query);

        while($info = mysql_fetch_array($result))
            {
            $content[] = $info;
            }

$count = count($content);

$result=array();

for($i=0;$i<$count;$i++)
{
    $result[id][] = $content[$i]['imageID'];
    $result[name][] = $content[$i]['Name'];
    $result[thumb][] = $content[$i]['Thumb'];
    $result[path][] = $content[$i]['Path'];
}

echo json_encode($result);

{"id":["1","2","3"],"name":["Dragon","fly","bug"],"thumb":["thm_polaroid.jpg","thm_default.jpg","thm_enhanced-buzz-9667-1270841394-4.jpg"],"path":["polaroid.jpg","default.jpg","enhanced-buzz-9667-1270841394-4.jpg"]}

But I am trying to format my array like so when it is encoded by json_encode.

[{"id":"1","name":"Dragon","thumb":"thm_polaroid.jpg","path":"polaroid.jpg"},{"id":"2","name":"Fly","thumb":"thm_default.jpg","path":"default.jpg"},{"id":"3","name":"Bug","thumb":"thm_enhanced-buzz-9667-1270841394-4.jpg","path":"enhanced-buzz-9667-1270841394-4.jpg"}]
3
  • Well the problem with your desired format is, that you have multiple times the string "image" as hash-key - this is not possible since every key has to be unique. Commented Jul 26, 2011 at 5:20
  • {"image":["1","Dragon","thm_polaroid.jpg","polaroid.jpg"],"image": - you have two (or more) identical keys (image). You REALLY want it? Commented Jul 26, 2011 at 5:22
  • No I do not need the repeat "image" key I am editing my post now. Commented Jul 26, 2011 at 5:25

3 Answers 3

4

Well, there is a problem. This is not valid JSON:

{"image":["1","Dragon","thm_polaroid.jpg","polaroid.jpg"],
 "image":["2","fly","thm_default.jpg","default.jpg"]}

A JSON object can only have one value per unique key. This means that your latter image key would clobber the value of the former.

If you are content with this, however:

[["1","Dragon","thm_polaroid.jpg","polaroid.jpg"],
 ["2","fly","thm_default.jpg","default.jpg"]]

Then you can simply use mysql_fetch_row:

 $result = $db->query($query);

 while($info = mysql_fetch_row($result))
 {
     $content[] = $info;
 }

 echo json_encode($content);

Side Note:

Generally, in PHP, it is best to use foreach( $arr as $val ) (or $arr as $key => $val). for loops should be limited to when they are strictly necessary.

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

Comments

1

You need to add the iterator $i to the setting array

    for($i=0;$i<$count;$i++)
    {
        $result[$i][id] = $content[$i]['imageID'];
        $result[$i][name] = $content[$i]['Name'];
        $result[$i][thumb] = $content[$i]['Thumb'];
        $result[$i][path] = $content[$i]['Path'];
    }

2 Comments

I knew it was something easy. You would laugh if you saw all the crazy stuff I have been trying. +1
This is not the solution that you asked for. This will give an associative array under each "image", instead of a numeric array.
0
<?
$result = $db->query($query);

while($info = mysql_fetch_array($result))
    $content[] = $info;
$result=array();
$count = count($content);
for ($x=0;$x<$count;++$x)
{
    $result[$x][] = $content[$x]['imageID'];
    $result[$x][] = $content[$x]['Name'];
    $result[$x][] = $content[$x]['Thumb'];
    $result[$x][] = $content[$x]['Path'];
}
echo json_encode($result);
?>

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.