Reputation: 67
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'];
$count++;
}
echo "<input id='json_pics' type='hidden' value='json_encode($pic_array)'/>";
Upvotes: 0
Views: 2001
Reputation: 8613
For better readability I would recommand using printf
for inserting the json encoded string.
echo sprintf("<input id='json_pics' type='hidden' value='%s'/>", json_encode($pic_array));
Upvotes: 1
Reputation: 54841
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.
Upvotes: 2