您好,我有一段带有数组示例的代码,问题是我丢失了数组的一个键。该数组是一个多维数组,我在这里发布了一个示例版本,因为其中有很多键(发布时间太长)
$columns = array(
"1" => array(
"foo" => "bar",
"bar" => "foo",
"foo" => "bar",
"bar" => "foo",
"foo" => "bar",
"bar" => "foo",
"foo" => "bar",
"bar" => "foo",
"foo" => "bar",
"bar" => "foo",
);
$array = array(
"1" => array(
"foo" => "bar",
"bar" => "foo",
"foo" => "bar",
"bar" => "foo",
"foo" => "bar",
"bar" => "foo",
"foo" => "bar",
"bar" => "foo",
"foo" => "bar",
"bar" => "foo",
),
"2" => array(
"1" => "bar",
"2" => "foo",
"3" => "bar",
"4" => "foo",
"5" => "bar",
"6" => "foo",
"7" => "bar",
"8" => "foo",
"9" => "bar",
"10" => "foo",
)
);
好的,在这两个数组中我有一个固定的数字或键,我想要实现的是从第一个数组中获取值并将它们用作我的第二个数组中的键。到目前为止一切都很好,但是当我执行第二个 foreach 以从第一个获取值并将它们作为键放在第二个中时,我从 $array 中丢失了我的键(我认为它被重写了)。你能帮我解决这个代码吗,因为我不知道如何修复它。
foreach($array as $key => $val)
{
array_splice($val, 66);
foreach($val as $k => $row)
{
if ( isset($columns[$k]))
{
$info[$columns[$k]] = $row;
}
}
return $info;
}
更新:
我想在这里实现的是创建一个数组,使用一个数组作为键,另一个数组作为值。后来发现我可以用array_combine来做。解决了我的问题