1

When I run :

while($r = mysql_fetch_assoc($result3)) { 

echo json_encode($r); 

}

the results is :

{"test1":"1","test2":"2","test3":"3","test4":"4"}

{"test1":"5","test2":"5","test3":"7","test4":"8"}

my question is : How can I create arrays from the results above knowing that the number of lines is not fixed, it may change depend on the data. So, I could make changes to the arrays and parse it in a format similar to this :

["test1"=>"1","5"],

["test2"=>"2","6"], 

["test3"=>"3","7"], 

["test4"=>"4", "8"]

Thanks!!

6
  • please print_r($result3); then put the result over here whatever it display its easy to help it out Commented Aug 21, 2012 at 7:08
  • @Jalpesh: $result3 is (presumably) a MySQL resource identifier. Commented Aug 21, 2012 at 7:09
  • 1
    @kech: One assumes that the change in the second value for test2 (from 5 in the current output to 6 in the desired output) is merely a typo? In any event, each array element has its own key so the desired output as shown doesn't really make sense: perhaps you are instead after something like { "test1" : [1,5], "test2" : [2,5], ... } ? Commented Aug 21, 2012 at 7:11
  • @eggyal Well, that's what I'm guessing as well... Commented Aug 21, 2012 at 7:13
  • @Jalpesh: yes - it's a MySQL resource identifier. print_r($r) gaves: ( [test1] => 1 [test2] => 2 [test3] => 3 [test4] => 4 ) Array ( [test1] => 5 [test2] => 6 [test3] => 7 [test4] => 8 ) Commented Aug 21, 2012 at 7:19

1 Answer 1

6

Try this :

<?php

$arr = array();

while ($r = mysql_fetch_assoc($result3)) 
{ 
    foreach ($r as $index=>$item)
    {
        $arr[$index][]=$item;
    }
}

?>

And to encode as JSON (after re-arranging the array) :

$arr = json_encode($arr);
Sign up to request clarification or add additional context in comments.

12 Comments

+1 For completeness, I'd also show where you perform the json_encode() here (i.e. after exiting the while loop).
@eggyal IF he actually wants it as Json. (my guess is that he used json only as a means of showing as the output)
Perhaps... but the question is tagged javascript and json, so my guess is not.
If I was to be really pedantic, I'd point out that your edit still doesn't clarify exactly where the call to json_encode() should be placed. But I'm not gonna do that. ;)
when I run print_r($arr[$index]); inside foreach I get the values without the keys : ( [0] => 1 ) Array ( [0] => 2 ) Array ( [0] => 3 ) Array ( [0] => 4 ) Array ( [0] => 5 ) Array ( [0] => 6 ) Array ( [0] => 7 ) Array ( [0] => 8 ) wherever I put $arr = json_encode($arr); inside or outside the while loop, when I run print_r($arr); it dispay ( [test1] => Array ( [0] => 5 ) [test2] => Array ( [0] => 6 ) [test3] => Array ( [0] => 7 ) [test4] => Array ( [0] => 8 ) )
|

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.