2017-07-24 46 views
-2

我有下面的代碼的問題:PHP - 如何通過JSON數組JSON陣列內重複

$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); 

這將返回:

{"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}} 

正如你可以看到我有主陣列內的一些數組。我想知道如何只訪問主數組中的數組? IE:事情是這樣的:

{"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} 
+0

你必須再次解碼該入分級陣列,然後迭代外數組的元素。或者訪問或搜索特定的元素,只要你喜歡。 – arkascha

+0

使用json_decode獲取一個json對象,然後能夠遍歷它像$ invoice_customers - > [「3」] - >名稱 – LordNeo

+0

您構建一個PHP數據結構,您將其編碼爲JSON,然後您想要什麼?您已擁有'$ invoice_customers'中的所有內容。你爲什麼要*「在主數組中訪問數組」*在一個字符串中? – axiac

回答

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);