我如何才能简化一个嵌套的PHP数组? [英] How can I simplify a nested php array?
问题描述
我在写一个PHP Web应用程序在那里我有一个嵌套的数组,类似于以下内容:
I'm writing a php web application where I have a nested array which looks similar to the following:
$results = array(
array(
array(
'ID' => 1,
'Name' => 'Hi'
)
),
array(
array(
'ID' => 2,
'Name' => 'Hello'
)
),
array(
array(
'ID' => 3,
'Name' => 'Hey'
)
)
);
目前,这意味着,当我要使用的ID字段我要叫 $结果[0] [0] ['身份证']
这是相当低效和拥有超过几百个记录的数组变得混乱很快。我想数组缩减下来,让我可以叫 $结果[0] ['身份证']
代替。
Currently this means that when I want to use the ID field I have to call $results[0][0]['ID']
which is rather inefficient and with an array of over several hundred records becomes messy quickly. I would like to shrink the array down so that I can call $results[0]['ID']
instead.
我的理解是,使用一个foreach循环遍历数组中的每一行进行迭代并更改格式的职能将是去改变 $结果的格式$的最佳方式C $ C>数组,但我努力理解要做什么foreach循环都有每个初始阵列后。
My understanding is that a function that uses a foreach loop to iterate through each row in the array and change the format would be the best way to go about changing the format of the $results
array but I am struggling to understand what to do after the foreach loop has each initial array.
下面是code我到目前为止有:
Here is the code I have so far:
public function filterArray($results) {
$outputArray = array();
foreach ($results as $key => $row) {
}
return $outputArray;
}
会有人能够提出最有效的方式来实现我追求的?
Would anyone be able to suggest the most effective way to achieve what I am after?
感谢:)
推荐答案
只需使用的 call_user_func_array
为
Simply use call_user_func_array
as
$array = call_user_func_array('array_merge', $results);
print_r($array);
这篇关于我如何才能简化一个嵌套的PHP数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!