0

I have the following codewhich basically seems to be overwriting the same array each time so the output (json) only gives me the last row returned?

Thanks

$structure = mysql_query("SELECT FIELDNAME, DISPLAYNAME from `_PREFS_MAINGRID_`");
$rowsField = array();
while($struct = mysql_fetch_assoc($structure)) {
    $rowsField["columname"] = $struct;
}


$plode = implode("` as `", $rowsField["columname"]);

print json_encode($rowsField);
0

1 Answer 1

4

You aren't pushing the elements into the array in your loop.

Try this:

$rowsField["columname"][] = $struct;

See the documentation for more information.

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

6 Comments

That's great! Thank you, it works now... Yes I will check the documentation on that. How can I then implode on this? Thanks
@user2816451: The same way you did it before
$plode = implode("` as ", $rowsField["columname"]); seems to return the work "Array" as its an object when printing to the browser rather than the contents of the array? When I did it before I had FIELDNAME as DISPLAYNAME` for example which is what I wanted, for each row returned
@realtek: The command isn't exactly the same -- it needs modifications. $rowsField is now a multi-dimensional array. Try doing a echo '<pre>,print_r($rowsField),'</pre>'; and see the what the array contains. And use the correct value in your implode().
thats great thank you. I re-wrote this using a foreach loop at the moment until I can figure out the implode on it. the printing of the array helped a lot! Thank you
|

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.