daneh daneh - 3 months ago 12x
JSON Question

Passing a php array as a json element with json_encode in a hidden input

I'm trying to work with an array in javascript so I am trying to Json_encode my php array as a hidden value. This is giving me this error Notice: Array to string conversion in.. Is this not possible? Am I going about this wrong?

$pic_array = array();
$titles = array();
$descriptions = array();
while ($row = $result->fetch_assoc()) {
$pic_array[$count] = $row['pic_url'];
$titles[$count] = $row['title'];
$descriptions[$count] = $row['description'];

echo "<input id='json_pics' type='hidden' value='json_encode($pic_array)'/>";


Proper code is

echo "<input id='json_pics' type='hidden' value='" . json_encode($pic_array) . "'/>";

In your current code php doesn't understand that you try to use json_encode function and just sees $pic_array variable which is array.