我有这个数组:
0 => array:3 [
"product_id" => "1138"
"product_image" => "/resources/medias/shop/products/shop-6500720--1.png"
"product_sku" => "6500722"
]
1 => array:3 [
"product_id" => "1144"
"product_image" => "/resources/medias/shop/products/shop-6501041--1.png"
"product_sku" => "6501046"
]
2 => array:3 [
"product_id" => "113"
"product_image" => "/resources/medias/shop/products/shop-6294909--1.png"
"product_sku" => "6294915"
]
我正在寻找一种获取仅包含必需列的多数组的方法(array_column
是不可行的,因为它只给我一列)。
我做了什么
function colsFromArray($array, $keys)
{
return array_map(function ($el) use ($keys) {
return array_map(function ($c) use ($el) {
return $el[$c];
}, $keys);
}, $array);
}
$array = array(
[
"product_id" => "1138",
"product_image" => "/resources/medias/shop/products/shop-6500720--1.png",
"product_sku" => "6500722"
],
[
"product_id" => "1144",
"product_image" => "/resources/medias/shop/products/shop-6501041--1.png",
"product_sku" => "6501046"
],
[
"product_id" => "113",
"product_image" => "/resources/medias/shop/products/shop-6294909--1.png",
"product_sku" => "6294915"
]
);
colsFromArray($array, array("product_id", "product_sku"));
//0 => array:3 [
// "product_id" => "1138"
// "product_sku" => "6500722"
// ]
//1 => array:3 [
// "product_id" => "1144"
// "product_sku" => "6501046"
// ]
//2 => array:3 [
// "product_id" => "113"
// "product_sku" => "6294915"
//]
问题在于它似乎太懒了,因为它对此进行了两次迭代。 没有此解决方法,有没有办法获取多列? PHP:5.6
答案 0 :(得分:3)
我认为更大的问题是您丢失了钥匙
array (
0 =>
array (
0 => '1138',
1 => '6500722',
),
1 =>
array (
0 => '1144',
1 => '6501046',
),
2 =>
array (
0 => '113',
1 => '6294915',
);
您可以使用简单的foreach代替第二个array_map:
function colsFromArray(array $array, $keys)
{
if (!is_array($keys)) $keys = [$keys];
return array_map(function ($el) use ($keys) {
$o = [];
foreach($keys as $key){
// if(isset($el[$key]))$o[$key] = $el[$key]; //you can do it this way if you don't want to set a default for missing keys.
$o[$key] = isset($el[$key])?$el[$key]:false;
}
return $o;
}, $array);
}
输出
array (
0 =>
array (
'product_id' => '1138',
'product_sku' => '6500722',
),
1 =>
array (
'product_id' => '1144',
'product_sku' => '6501046',
),
2 =>
array (
'product_id' => '113',
'product_sku' => '6294915',
),
)
问题在于它似乎太懒了,因为它对此进行了两次迭代。
没有真正的方法可以不对其进行2次迭代,但是您也可能不想丢掉这些键。
那表示您可以递归地取消设置不需要的项目。
function colsFromArray(array &$array, $keys)
{
if (!is_array($keys)) $keys = [$keys];
foreach ($array as $key => &$value) {
if (is_array($value)) {
colsFromArray($value, $keys); //recursive
}else if(!in_array($key, $keys)){
unset($array[$key]);
}
}
}
colsFromArray($array, array("product_id", "product_sku"));
var_export($array);
与以前相同的输出
通过引用更容易做到这一点。不管是否更快,您都必须测试2并查看。
最后一点,除非您键入将其强制转换为数组,否则您不应该假设该键将存在或该键将是一个数组。
您也可以使用数组过滤器
function colsFromArray(array $array, $keys)
{
if (!is_array($keys)) $keys = [$keys];
$filter = function($k) use ($keys){
return in_array($k,$keys);
};
return array_map(function ($el) use ($keys,$filter) {
return array_filter($el, $filter, ARRAY_FILTER_USE_KEY );
}, $array);
}
声明循环外过滤函数(array_map)有一些小的性能好处。
答案 1 :(得分:2)
如果您需要数组中的两列,其中一列是SKU(通常是唯一的),则可以将array_column与第三个参数一起使用。
$new = array_column($arr, "product_id", "product_sku");
这将返回一个平面数组,其中SKU作为键,ID作为值,使该数组也易于使用。
输出:
array(3) {
[6500722]=>
string(4) "1138"
[6501046]=>
string(4) "1144"
[6294915]=>
string(3) "113"
}
答案 2 :(得分:1)
如果我正确理解了您的问题,则可以尝试使用传统的foreach-可能会更快一些。
function colsFromArray($array, $filterKeys) {
$newArr = [];
foreach($array as $key => $val) {
$element = [];
foreach($filterKeys as $filterKey) {
$element[$filterKey] = $val[$filterKey];
}
$newArr[] = $element;
}
}
(未经测试)
问题在于它似乎太迟了,因为它对此进行了两次迭代
您的原始代码未在同一数组上重复两次。如果要有一个数组,其中每个元素是另一个元素数组,这些元素带有filterKeys数组中的键,则将无法遍历主数组,然后遍历filterKeys数组。
答案 3 :(得分:1)
如果您不想更改原始数组并想要所需的输出
使用array_insersect_key函数获得所需的输出,如下所示:
$array = array(
[
"product_id" => "1138",
"product_image" => "/resources/medias/shop/products/shop-6500720--1.png",
"product_sku" => "6500722"
],
[
"product_id" => "1144",
"product_image" => "/resources/medias/shop/products/shop-6501041--1.png",
"product_sku" => "6501046"
],
[
"product_id" => "113",
"product_image" => "/resources/medias/shop/products/shop-6294909--1.png",
"product_sku" => "6294915"
]
);
$keys = array("product_id"=>1, "product_sku"=>2);
$filteredArray = array_map(function($a) use($keys){
return array_intersect_key($a,$keys);
}, $array);
print_r($filteredArray);
答案 4 :(得分:1)
我将@Chayan的优雅方法重构为一个函数,以便可以像array_column()
那样使用它。现在可以将要过滤的键显示为一个简单的数组。
这是最有可能也是最快的方法,因为它使用内置功能来完成大多数繁重的工作。
<?php
function array_columns(array $arr, array $keysSelect)
{
$keys = array_flip($keysSelect);
$filteredArray = array_map(function($a) use($keys){
return array_intersect_key($a,$keys);
}, $arr);
return $filteredArray;
}
$arr = array(
[
"product_id" => "1138",
"product_image" => "/resources/medias/shop/products/shop-6500720--1.png",
"product_sku" => "6500722"
],
[
"product_id" => "1144",
"product_image" => "/resources/medias/shop/products/shop-6501041--1.png",
"product_sku" => "6501046"
],
[
"product_id" => "113",
"product_image" => "/resources/medias/shop/products/shop-6294909--1.png",
"product_sku" => "6294915"
]
);
$keysSelect = array("product_id" , "product_sku");
$filteredArray = array_colums($arr, $keysSelect);
var_dump($filteredArray);
答案 5 :(得分:0)
这是基于Chayan的重构函数,并添加了对选定列的重命名:
/** Function - array_columns Selects columns from multidimantional array and renames columns as required
*
* @param array $arr, array $selectColRenameKeys
* example: (NewName1->colNameneeded1,NewName2->colNameneeded2,ect...)
* @return array
* @access public
*
*/
private function array_columns( $arr,$selectColRenameKeys) {
$keys = array_flip($selectColRenameKeys);
$filteredArray = array_map(function($a) use($keys){
$data = array_intersect_key($a,$keys);
$rename_arr= array();
foreach ($data as $colname => $value){
$r_arr[$keys[$colname]]= $value ;
}
return $r_arr;
}, $arr);
return $filteredArray;
}
答案 6 :(得分:0)
array_columns函数的附加功能最终可以追溯到Chayan的答案,这次是从Joseph Mangion的函数扩展而来。
我有时会在选定列的列表中列出一长串,我想在其中保留键,而不必在大量字段中遵循繁琐的['orignal_field_name'] => ['original_field_name']
格式。
此版本默认情况下会保留每个字段的原始密钥,除非指定了新密钥。
// See answer from Joseph Mangion: https://stackoverflow.com/questions/52706383/php-get-multiple-columns-from-array
/** Function - array_columns Selects columns from multidimensional array and renames columns as required
*
* @param array $in_array, array $select_columns_rename_keys
* example of $select_columns_rename_keys:
* ['new_column_name1' => 'original_column_name1', 'original_column_name2', 'original_column_name3', 'new_column_name4' => 'original_column_name4', ...]
* This will use the original keys for columns 2 and 3 and rename columns 1 and 4
* @return array
* @access public
*
*/
public function array_columns($in_array, $select_columns_rename_keys) {
foreach ($select_columns_rename_keys as $k => $v)
if (is_int($k)) {
$select_columns_rename_keys[$v] = $v;
unset($select_columns_rename_keys[$k]);
}
$keys = array_flip($select_columns_rename_keys);
$filtered_array =
array_map(function($a) use($keys) {
$data = array_intersect_key($a, $keys);
$return_array = [];
foreach ($data as $column_name => $value) $return_array[$keys[$column_name]] = $value;
return $return_array;
}, $in_array);
return $filtered_array;
}