如何在jQuery中获取PHP数组值

时间:2021-08-23 13:43:35

I have a PHP script that sends this array to the client via ajax:

我有一个PHP脚本,通过ajax将此数组发送到客户端:

Array
(
    [0] => $209.90
    [1] => $20.99
    [2] => $188.91
)

I am sending the array to the client this way:

我通过这种方式将数组发送到客户端:

return $servicePrices;

I have this jQuery statement:

我有这个jQuery语句:

success: function(data) {
  //console.log('Success'+data);
  $( '#tourSubtotal' ).val( data[0] );
  $( '#tourDiscount' ).val( data[1] );
  $( '#tourTotal' ).val( data[2] );
},

My results for each ID value are A, r and r. Instead, how can I get the 3 currency data values from the array in my ajax jQuery?

我对每个ID值的结果是A,r和r。相反,如何从我的ajax jQuery中获取数组中的3个货币数据值?

2 个解决方案

#1


7  

It looks like you are using print_r() function to display this:

看起来您正在使用print_r()函数来显示:

Array
(
    [0] => $209.90
    [1] => $20.99
    [2] => $188.91
)

In that format it's just as useful as any string is. You have to use json_encode() to convert it to a format both languages understand like:

在那种格式中,它与任何字符串一样有用。您必须使用json_encode()将其转换为两种语言都理解的格式:

["$209.90", "$20.99", "$188.91"]

Your PHP code should be something like:

你的PHP代码应该是这样的:

<?php
  $json = some_value;
  // Remove this:
  // print_r($json);
  // Instead, write this:
  echo json_encode($json);
?>

And then you can use it in JavaScript, the way you have specified. Also, jQuery is smart enough to understand that the response is JSON and you don't need JSON.parse() in most of the cases.

然后,您可以按照指定的方式在JavaScript中使用它。此外,jQuery足够聪明,可以理解响应是JSON,并且在大多数情况下您不需要JSON.parse()。

#2


3  

You can return the array in a JSON encoded format from PHP as mentioned below:

您可以从PHP返回JSON编码格式的数组,如下所述:

$result = array(
    '0' => '$209.90'
    '1' => '$20.99'
    '2' => '$188.91'
);
return json_encode($result);

And on the AJAX success function you can use :

在AJAX成功函数上,您可以使用:

success: function(data) {
   var temp = JSON.parse(data);
   $( '#tourSubtotal' ).val( temp[0] );
   $( '#tourDiscount' ).val( temp[1] );
   $( '#tourTotal' ).val( temp[2] );
},

Hope this helps.

希望这可以帮助。

#1


7  

It looks like you are using print_r() function to display this:

看起来您正在使用print_r()函数来显示:

Array
(
    [0] => $209.90
    [1] => $20.99
    [2] => $188.91
)

In that format it's just as useful as any string is. You have to use json_encode() to convert it to a format both languages understand like:

在那种格式中,它与任何字符串一样有用。您必须使用json_encode()将其转换为两种语言都理解的格式:

["$209.90", "$20.99", "$188.91"]

Your PHP code should be something like:

你的PHP代码应该是这样的:

<?php
  $json = some_value;
  // Remove this:
  // print_r($json);
  // Instead, write this:
  echo json_encode($json);
?>

And then you can use it in JavaScript, the way you have specified. Also, jQuery is smart enough to understand that the response is JSON and you don't need JSON.parse() in most of the cases.

然后,您可以按照指定的方式在JavaScript中使用它。此外,jQuery足够聪明,可以理解响应是JSON,并且在大多数情况下您不需要JSON.parse()。

#2


3  

You can return the array in a JSON encoded format from PHP as mentioned below:

您可以从PHP返回JSON编码格式的数组,如下所述:

$result = array(
    '0' => '$209.90'
    '1' => '$20.99'
    '2' => '$188.91'
);
return json_encode($result);

And on the AJAX success function you can use :

在AJAX成功函数上,您可以使用:

success: function(data) {
   var temp = JSON.parse(data);
   $( '#tourSubtotal' ).val( temp[0] );
   $( '#tourDiscount' ).val( temp[1] );
   $( '#tourTotal' ).val( temp[2] );
},

Hope this helps.

希望这可以帮助。