如何将数据php更改为javascript json [关闭]

I have looping data:

image1
url1
description1

image2
url2
description2

image3
url3
description3

image4
url4
description4

image5
url5
description5

Result:

var imagesDataArray = [ 
   {
      src: 'images1',
      url: 'url1',
      description: 'description1'
   },
   {
      src: 'images2',
      url: 'url2',
      description: 'description2'
   },
   {
      src: 'images3',
      url: 'url3',
      description: 'description3',
   },
   {
      src: 'images4',
      url: 'url4',
      description: 'description4'
   },
   {
      src: 'images5',
      url: 'url5',
      description: 'description5'
   }
];

It seems like you want to encode your PHP array to JSON. Use json_encode() for that.

json_encode($array);

Returns a string containing the JSON representation of value.

If backend is under your control instead of returning plain data use, json_encode($data) and return json. In JS All you have to load the response into a variable and manipulate it according to your needs. Returning plain data from backend is unnecessary code complication inside javascript.

Put this in your php file, it will return a string containing the JSON.

<?php
echo '<script>';
echo 'var $data = ' . json_encode($phpdata) . ';';
echo '</script>';
?>

Then use the variable $data for your code.