PHP-从数组获取多个列 [英] PHP - get multiple columns from array

查看:680
本文介绍了PHP-从数组获取多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数组:

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不是一种选择,因为它只给我一列).

What I am looking for is a way to get a multiple array with only required columns (array_column is not a option, since it's give me only 1 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

The problem is that it seems too laggy, since it iterates twice over this. Is there any way to get multiple columns without this workaround? PHP: 5.6

推荐答案

我认为更大的问题是您丢失了钥匙

I think the bigger issue is you lose the keys

原始代码

array (
  0 => 
  array (
    0 => '1138',
    1 => '6500722',
  ),
  1 => 
  array (
    0 => '1144',
    1 => '6501046',
  ),
  2 => 
  array (
    0 => '113',
    1 => '6294915',
 );

您可以使用简单的foreach代替第二个array_map:

You can use a simple foreach instead of the second 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',
  ),
)

沙盒

问题在于它似乎太迟了,因为它对此进行了两次迭代.

the problem is that it seems too laggy, since it iterates twice over this.

没有真正的方法可以不对其进行2次迭代,但是您也可能不想丢掉这些键.

There is no real way to not iterate over it 2 times, but you probably don't want to throw away the keys either.

那表示您可以递归地取消设置不需要的项目.

That said you can recursively unset the items you don't want.

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并查看.

This is easier to do by reference. Rather or not that is faster you'll have to test the 2 and see.

沙盒

作为最后一点,除非您键入将其转换为数组,否则您不应该假定该键将存在或该键将是一个数组.

As a final note you shouldn't assume the key will exist or that keys will be an array unless you type cast it as an array.

您也可以使用数组过滤器

You could also do it with array filter

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)有一些小的性能好处.

There is some small performance benefit to declaring the function for filtering outside of the loop (array_map).

沙盒

这篇关于PHP-从数组获取多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆