CakePHP带有方法提取/组合的哈希类 [英] CakePHP Hash class with method extract/combine
问题描述
什么是数组提取的解决方案
我有数组:
$ arr = array(
'key1'=> array(
'name'=>'Value 1'
),
'key2'=& array(
'name'=>'Value 2'
)
);
我想得到这样的结果:
数组
(
[key1] =>值1
[key2] =>值2
)
在Cake 2.2中, Set :: extract('{\w +})但是当我想使用Hash by Hash :: extract($ arr,'{\w +)。name')
结果不是核心(也 Hash :: extract($ arr,'{s} .name')
返回incorect。
如何使用新的Hash类?
解决方案 //stackoverflow.com/users/1645066/anil-kumar\">@Anil kumar 的答案
如果您的结果集之一返回空数组,这将防止未定义的索引break:
$ arr = Hash :: map($ arr,'',function $ newArr){
return Hash :: get($ newArr,'name');
});
What is solution of extract to array
I have array:
$arr = array(
'key1' => array(
'name' => 'Value 1'
),
'key2' => array(
'name' => 'Value 2'
)
);
I would like get results like this:
Array
(
[key1] => Value 1
[key2] => Value 2
)
In Cake 2.2 and lower works by Set::extract('{\w+}.name', $arr)
but when I would like use Hash by Hash::extract($arr, '{\w+).name')
the results not corectly (also Hash::extract($arr, '{s}.name')
return incorect.
How do this using new Hash class?
解决方案 just a follow up on @Anil kumar's answer
in case one of your result set returns an empty array, this would prevent an undefined index break:
$arr = Hash::map($arr, '', function($newArr) {
return Hash::get($newArr, 'name');
});
这篇关于CakePHP带有方法提取/组合的哈希类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!