[英]Loop through an array in PHP
有沒有辦法在數組中引用數組的值?
例如。
我有以下 php 代碼來循環遍歷下面列出的數組。 我可以在沒有另一個 for 循環的情況下輕松獲取[product_name] => DIESEL #2
數據嗎? 如果我知道數據總是會這樣? 像 $ fuel = $value['transaction_id'.'product'.'product_name']
?
foreach ($result['data'] as $key => $value) {
$id = $value['id'];
$transactionId = "FC".$value['transaction_id'];
$transactionDateTime = $value['created_utc'];
echo $id." - ".$transactionDateTime." - ".$transactionId." </br>";
}
(
[data] => Array
(
[0] => Array
(
[id] => 5750574
[label] =>
[self] => https://api.fuelcloud.com/rest/v1.0/transaction/5750574
[transaction_id] => 0025621e-3270-c0e2-8d2a-6425ed834f9d
[transaction_type] => 1
[tank_id] => 23744
[start_date] => 2019-12-19T00:00:00
[end_date] => 2019-12-19T23:59:59Z
[site_id] => 23743
[product_id] => 409
[product] => Array
(
[id] => 409
[product_name] => DIESEL #2
[product_category] => diesel
)
您似乎已經知道如何引用數組條目,例如$value['id']
。 $value['product']
是另一個數組, $value['product']['product_name']
是該數組中product_name
鍵的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.