0

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)'/>";
1
  • The json_encode has to be outside quotes, for php to execute it. echo "<input id='json_pics' type='hidden' value='".json_encode($pic_array)."'/>"; Commented Jun 27, 2016 at 18:21

2 Answers 2

2

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.

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

Comments

1

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));

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.