我遇到以下代码问题:
Query q =session.createQuery("select u from User u where u.location=? and u.id in (:user_ids)");
q.setParameter(0, location);
q.setParameterList("user_ids", userIds);
return q.list();
返回:
$invoice_customers = [];
foreach ($new['data'] as $key=>$display) {
if ($display['customer'] == $customer){
$invoice_customers[$key]['customer'] = $display['source']['customer'];
$invoice_customers[$key]['name'] = $display['source']['name'];
$invoice_customers[$key]['amount'] = $display['amount'];
$invoice_customers[$key]['refunded'] = $display['refunded'];
$invoice_customers[$key]['last4'] = $display['source']['last4'];
$invoice_customers[$key]['id'] = $display['id'];
$invoice_customers[$key]['created'] = $display['created'];
}
}
echo json_encode($invoice_customers);
正如你所看到的,我的主阵列中有一些数组。我想知道如何只访问主数组中的数组? IE:像这样:
{"3":
{"customer":"xxxxx","name":"Testtt Test","amount":100,"refunded":false,"last4":"4242","id":"x","created":1500342123},
"4xxx":
{"customer":"xxxxx","name":"Testtt Test","amount":100,"refunded":false,"last4":"4242","id":"xxxx","created":1500340979}}
答案 0 :(得分:2)
您现有的对象
var myobject ={
"3":{
"customer":"xxxxx",
"name":"Testtt Test",
"amount":100,
"refunded":false,
"last4":"4242",
"id":"x",
"created":1500342123
},
"4xxx":{
"customer":"xxxxx",
"name":"Testtt Test",
"amount":100,
"refunded":false,
"last4":"4242",
"id":"xxxx",
"created":1500340979
}
}
可以像myobject['3'], myobject['4xxx']
如果要创建数组
,请使用下面的一个json_encode(array_values($invoice_customers));
将生成,并且可以使用索引0,1....
进行访问,如myarray[0], myarray[1] ...
var myarray =[
{
"customer":"xxxxx",
"name":"Testtt Test",
"amount":100,
"refunded":false,
"last4":"xxxx",
"id":"x",
"created":1500342123
},
{
"customer":"xxxxx",
"name":"Testtt Test",
"amount":100,
"refunded":false,
"last4":"xxxx",
"id":"xxxx",
"created":1500340979
}
]
以下是演示
var myobject ={
"3":{
"customer":"xxxxx",
"name":"Testtt Test",
"amount":100,
"refunded":false,
"last4":"4242",
"id":"x",
"created":1500342123
},
"4xxx":{
"customer":"xxxxx",
"name":"Testtt Test",
"amount":100,
"refunded":false,
"last4":"4242",
"id":"xxxx",
"created":1500340979
}
};
var myarray =[
{
"customer":"xxxxx",
"name":"Testtt Test",
"amount":100,
"refunded":false,
"last4":"xxxx",
"id":"x",
"created":1500342123
},
{
"customer":"xxxxx",
"name":"Testtt Test",
"amount":100,
"refunded":false,
"last4":"xxxx",
"id":"xxxx",
"created":1500340979
}
];
// access object
console.log(myobject['3']['customer']);
console.log(myobject['3'].name);
// access array
console.log(myarray[0]['customer']);
console.log(myarray[0].name);