如何使用 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]
];
所以这是名为 $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]
];