php 取出多重数组中的一列,PHP-从数组获取多个列

I have this array:

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"

]

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).

What I have done

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"

//]

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',

);

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);

}

Output

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.

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);

Same output as before

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);

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值