I am struggling with this for some reason.
我出于某种原因正在努力解决这个问题。
I have 2 arrays. The first is a standard array called colsArray that looks like this:
我有2个阵列。第一个是名为colsArray的标准数组,如下所示:
Array
(
[0] => fName
[1] => lName
[2] => city
)
The second is a multidimensional array called query_data that looks like this:
第二个是名为query_data的多维数组,如下所示:
Array
(
[0] => Array
(
[recordID] => xxx
[fName] => xxx
[lName] => xxx
[address1] => xxx
[city] => xx
[zip] => xxx
[vin] => xxx
)
[1] => Array
(
[recordID] => xxx
[fName] => xxx
[lName] => xxx
[address1] => xxx
[city] => xxx
[zip] => xxx
[vin] => xxx
)
[2] => Array
(
[recordID] => xxx
[fName] => xxx
[lName] => xxx
[address1] => xxx
[city] => xxx
[zip] => xxx
[vin] => xxx
)
[3] => Array
(
[recordID] => xxx
[fName] => xxx
[lName] => xxx
[address1] => xxx
[city] => xxx
[zip] => xxx
[vin] => xxx
)
)
)
I just need to use those 2 arrays to create a new array that has all the data from the query_data array where the keys are in the fist array colsArray. The new array would look like this:
我只需要使用这两个数组来创建一个新数组,该数组包含来自query_data数组的所有数据,其中键位于第一个数组colsArray中。新数组看起来像这样:
Array
(
[0] => Array
(
[fName] => xxx
[lName] => xxx
[city] => xx
)
[1] => Array
(
[fName] => xxx
[lName] => xxx
[city] => xx
)
[2] => Array
(
[fName] => xxx
[lName] => xxx
[city] => xx
)
[3] => Array
(
[fName] => xxx
[lName] => xxx
[city] => xx
)
)
Any help on this would be great.
对此的任何帮助都会很棒。
Thanks!
谢谢!
2 个解决方案
#1
8
PHP offers a variety of array functions, and you usually get away with just combining a bunch of those:
PHP提供了各种各样的数组函数,你通常只需将这些函数组合在一起就可以了:
$keys = array_flip($colsArray);
$new_data = array();
foreach($query_data as $key => $data) {
$new_data[$key] = array_intersect_key($data, $keys);
}
Alternatively, a more functional style, but more memory intensive:
或者,功能更强大,但内存更密集:
$new_data = array_map(
'array_intersect_key', // or just array_intersect_key in newer PHP versions
$query_data,
array_pad(array(), count($query_data), array_flip($colsArray))
);
#2
1
$finalarr = [];
foreach ($query_data as $data) {
$arr = [];
foreach ($colsArray as $key){
$arr[$key] = $data[$key];
}
$finalarr[] = $arr;
}
The []
notation for creating arrays is new, so you might have to use array()
instead.
创建数组的[]表示法是新的,因此您可能必须使用array()。
#1
8
PHP offers a variety of array functions, and you usually get away with just combining a bunch of those:
PHP提供了各种各样的数组函数,你通常只需将这些函数组合在一起就可以了:
$keys = array_flip($colsArray);
$new_data = array();
foreach($query_data as $key => $data) {
$new_data[$key] = array_intersect_key($data, $keys);
}
Alternatively, a more functional style, but more memory intensive:
或者,功能更强大,但内存更密集:
$new_data = array_map(
'array_intersect_key', // or just array_intersect_key in newer PHP versions
$query_data,
array_pad(array(), count($query_data), array_flip($colsArray))
);
#2
1
$finalarr = [];
foreach ($query_data as $data) {
$arr = [];
foreach ($colsArray as $key){
$arr[$key] = $data[$key];
}
$finalarr[] = $arr;
}
The []
notation for creating arrays is new, so you might have to use array()
instead.
创建数组的[]表示法是新的,因此您可能必须使用array()。