![](/img/trans.png)
[英]How can I add a $key=>$value pair to an associative array using a foreach loop in PHP?
[英]how can I restructure an associative array using a PHP foreach loop (or not)?
所以這里是一個名為 $results 的關聯數組的第一個鍵:
[ 10 (2) => [ step (3) => [ 0 (1) => [ id => 1 ], 1 (1) => [ id => 2 ], 2 (1) => [ id => 3 ] ], status (3) => [ 0 (1) => [ id => 2 ], 1 (1) => [ id => 4 ], 2 (1) => [ id => 10 ] ] ],
我想對其進行重組,使其看起來像這樣:
[ 10 (2) => [ step (1) => [ 0 (1) => [ id => 1 ], status (1) =>[ 0 (1) => [ id => 2 ], step (1) => [ 0 (1) => [ id => 2 ], status (1) =>[ 0 (1) => [ id => 4 ], AND SO ON...
簡而言之,我只想將每個步驟的步驟和狀態顯示為一對,並產生一個 [step, status], [step, status], [step, status]... 關聯數組。 目前,數組更像 [step, step step], [status, status, status]。
這是我最初的 foreach 循環,它首先給了我數組:
$results = []; foreach ($entities['node'] as $nodeIdBis => $nodeWkf) { $nodeWkfTmp = ["step" => [], "status" => []]; foreach ($nodeWkf as $wkfId => $subNodeWkf) { foreach ($subNodeWkf as $stepId => $nodeStatus) { $nodeWkfTmp["status"][] = ["id" => $nodeStatus['statusId']]; $nodeWkfTmp["step"][] = ["id" => $stepId]; } } $results[$nodeIdBis] = $nodeWkfTmp; }
非常尊重誰會找到訣竅:)
只需更改 $nodeWkfTmp 數組賦值即可獲得一對數組 [step, status]:
$nodeWkfTmp = [];
$nodeWkfTmp[] = [
"status" => ["id" => $nodeStatus['statusId']],
"step" => ["id" => $stepId]
];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.