簡體   English   中英

如何使用 PHP foreach 循環(或不使用)重構關聯數組?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM