如何在 Javascript 中将对象从 PHP 转换为数组
How to convert an object from PHP to an Array in Javascript?
我正在尝试将从 PHP 获得的对象转换为 Javascript 中的数组,但我不知道如何解决这个问题,我尝试了一些我在做这个问题之前阅读的解决方案,但它不起作用,我在 PHP 中得到了这样的东西
$data = explode(',', $articles);
$length = count($data);
for ($i = 0; $i < $length; $i++) {
$products = explode('-', $data[$i]);
$product_key = $products[0];
$quantity = $products[1];
$get_data_product = mysqli_query($connection, "SELECT * FROM articles WHERE id_article = '".$product_key."' AND id_user = '".$id_user."'") or die (mysqli_error($connection));
$get_data_product = mysqli_fetch_assoc($get_data_product);
$article_name = $get_data_product['name'];
$article_price = $get_data_product['price'];
$info[$i] = array('name' => $article_name, 'quantity' => $quantity, 'price' => $article_price);
}
echo json_encode($info);
在Javascript中
success: function(info) {
var data = JSON.stringify(info);
console.log(data);
}
控制台日志显示此内容
[{"name":"prueba 2","quantity":"4","price":"100"}]
我试图用代码读取它们,但它显示数据,只有当我使用控制台日志或警报时
$.each(data, function(i) {
$('#content').append('<tr class="text-center">' +
'<td>' + data[i].quantity + '</td>' +
'<td>' + data[i].name + '</td>' +
'</tr>');
});
正如prodigitalson在评论中指出的那样 - 并且实际上是答案 - 您将数据转换为PHP中的JSON对象,但随后不必要地将其转换为字符串,因此您根本无法操作它。
例如:JSON.stringify([{test: 1}])
变成"[{test: 1}]"
。
您将能够以原始方式循环它,或者您可以使用标准的 javascript for each 循环它,其中data
是您在回调中的响应,其中包含[{"name":"prueba 2","quantity":"4","price":"100"}]
:
data.forEach(function (e) {
$('#content').append(
'<tr class="text-center">'n' +
'<td>' + e.quantity + '</td>'n' +
'<td>' + e.name + '</td>'n' +
'</tr>'n'
);
});
PHP 端示例代码:
<?php
for ($i = 0; $i < 3; $i++) {
$quantity=rand(5,10);
$price=rand(500,1000);
$info[$i] = array('name' =>'prueba'.$i, 'quantity' =>"$quantity", 'price' => "$price");
}
echo json_encode($info);
?>
JQuery 结束:
<!DOCTYPE html>
<html>
<head>
<title>Json</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({ type:"POST", url: "array.php", dataType: "json", success: function(result){
console.log(result);
for(i=0; i<result.length;i++)
{
console.log(result[i]);
console.log(result[i].name);
console.log(result[i].quantity);
console.log(result[i].price);
}
}});
});
</script>
</head>
<body>
</body>
</html>
测试中的示例响应:
[{"name":"prueba0","quantity":"8","price":"574"},{"name":"prueba1","quantity":"8","price":"555"},{"name"
:"prueba2","quantity":"7","price":"901"}]
参考:http://www.jsoneditoronline.org/?id=5e23cd942a949e86545fa320a65ff0e7
希望这能解决您的问题。谢谢。
我认为你的代码很好,如果你在javascript代码中这样做:
success: function(info) {
var data = JSON.stringify(info);
alert(data[0].name);
}
该警报应显示数据对象的 name 属性的预期值。
你的控制台.log(data); 显示 json arry 那是[{"名称":"普鲁巴 2","数量":"4","价格":"100"}]
试试这个
$dataobj = 数据1=json_decode(数据);
$arry = (数组) $dataobj;
试试这个代码
- 使用Javascript将PHP转换为HTML5
- 如何将JSON从PHP转换为JS
- 将PHP转换为JavaScript,自调用函数
- 从sqlite PHP转换数据
- 注意:将数组从 php 转换为 javascript 时出现数组到字符串的转换错误
- 使用 PHP 转换 SQL 时间
- 如何将其从PHP转换为JavaScript
- 将数组从php转换为javascript
- php转换为javascript并返回
- 将jsonencode php转换为javascript,但不要使用双引号
- 将PHP转换为javascript #1
- 将逗号php转换为javascript
- 用PHP转换知名文本(WKT)从MySQL到谷歌地图多边形
- 将javascript数组从PHP转换为javascript函数
- 通过PHP转换XML &Javascript函数
- 将数学方程式从PHP转换为Javascript (Jquery)
- 使用php转换为json
- 如何将格式日期从PHP转换为Javascript
- 如何将这个PHP转换为express.js/node
- 用于转换从 PHP 转换的序列号的 JavaScript 代码