下面的数组应该为从 1 到 7 的每个“点”值创建一个子数组,但缺少 2、4 和 6。如何为每个缺失值插入一个数组,“值”为 NULL?
我在我的数组上尝试了一个for($i = 1; $i <= 7; $i++)
and inside foreach'ing 来查看是否'point' == $i
,但这不起作用。
这已经在一个相当大的数组的嵌套 foreach 中,所以性能有点重要。
到目前为止的代码(不完整,显然不起作用)
foreach($measurements as $measurement_id => $set)
{
for($i = 1; $i <= 7; $i++)
{
foreach($set as $key => $value)
{
$fill = array('value' => null);
if($value['point'] == $i)
$output[$measurement_id][$key] = $value;
else
$output[$measurement_id][$key] = $fill;
}
}
}
数组:
[0] => Array
(
[point] => 1
[value] => 1.0
)
[1] => Array
(
[point] => 3
[value] => 2.0
)
[2] => Array
(
[point] => 5
[value] => 3.0
)
[3] => Array
(
[point] => 7
[value] => 4.0
)
结果应该是
[0] => Array
(
[point] => 1
[value] => 1.0
)
[1] => Array
(
[point] => 2
[value] => NULL
)
[2] => Array
(
[point] => 3
[value] => 2.0
)
[3] => Array
(
[point] => 4
[value] => NULL
)
[4] => Array
(
[point] => 5
[value] => 3.0
)
[5] => Array
(
[point] => 6
[value] => NULL
)
[6] => Array
(
[point] => 7
[value] => 4.0