Is there a way to pass multiple arrays to PHP json_encode and parse it with jQuery?

后端 未结 4 1289
眼角桃花
眼角桃花 2020-12-13 15:40

Right now I have this PHP:

$columns = array(*/Data*/);
echo json_encode($columns);

And this is sent through an AJAX GET request with JQuery

4条回答
  •  抹茶落季
    2020-12-13 16:27

    Sure, you could send an array of array. PHP associative array will become a javascript object.

    In PHP:

    $data = array();
    $data['fruits'] = array('apple','banana','cherry');
    $data['animals'] = array('dog', 'elephant');
    echo json_encode($data);
    

    and then on jQuery

    var data = jQuery.parseJSON(response);
    

    then you could then do something like this to access the values

    console.log(data.fruits[0]); // apple
    console.log(data.animals[1]); // elephant
    

提交回复
热议问题