Isi*_*ius 6 php associative-array
根据https://wiki.php.net/rfc/array_column,array_column将很快添加到PHP中.但我无法理解RFC.如果命名密钥不存在,将返回什么?
例:
$arr = array(
array(
'firstname' => 'Bob',
'lastname' => 'Tomato'
),
array(
'firstname' => 'Larry',
'lastname' => 'Cucumber'
)
);
$middlenames = array_column($arr, 'middlename');
Run Code Online (Sandbox Code Playgroud)
介绍
让您了解RFC您需要首先了解问题及其引入的原因.
你的数组
$arr = array(
array(
'firstname' => 'Bob', <--
'lastname' => 'Tomato' | <--
), | |
array( | |
'firstname' => 'Larry', <-- |
'lastname' => 'Cucumber' <-|
)
);
Run Code Online (Sandbox Code Playgroud)
获得专栏
要获得Bob & Larry或Tomato and Cucumber使用多行代码示例,请执行以下操作:
$colums = array();
foreach ( array_map(null, $arr[0], $arr[1]) as $value ) {
$colums[] = $value;
}
print_r($colums);
Run Code Online (Sandbox Code Playgroud)
Array
(
[0] => Array
(
[0] => Bob
[1] => Larry
)
[1] => Array
(
[0] => Tomato
[1] => Cucumber
)
)
Run Code Online (Sandbox Code Playgroud)
动态版
只有当您知道另一种创造性方式的元素数量时,上述代码才有效
$colums = array();
array_unshift($arr, null);
foreach (call_user_func_array("array_map", $arr) as $value ) {
$colums[] = $value;
}
Run Code Online (Sandbox Code Playgroud)
或者更好的Sill使用 MultipleIterator
$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ALL);
foreach ( $arr as $v ) {
$mi->attachIterator(new ArrayIterator($v));
}
$colums = array();
foreach ( $mi as $v ) {
$colums[] = $v;
}
print_r($colums);
Run Code Online (Sandbox Code Playgroud)
密钥名称
如果你需要获取密钥名称,这是另一种方法
$colums = array_reduce($arr, function ($a, $b) {
foreach ( $b as $k => $v ) {
$a[$k][] = $v;
}
return $a;
});
Run Code Online (Sandbox Code Playgroud)
回到 array_column
array_column 只想简单地处理过程并获取具有名字的所有列将如下所示简单:
print_r(array_column($arr,"lastname"));
^
|
+--- This get value with key "lastname"
Run Code Online (Sandbox Code Playgroud)
更复杂的Senerio
想象一下,您希望您的阵列具有此输出
Array
(
[Bob] => Tomato
[Larry] => Cucumber
)
Run Code Online (Sandbox Code Playgroud)
使用您可以使用的旧方法
$colums = array();
array_unshift($arr, null);
foreach (call_user_func_array("array_map", $arr) as $value ) {
$key = array_shift($value);
$colums[$key] = current($value);
}
print_r($colums);
Run Code Online (Sandbox Code Playgroud)
现在你可以看到我必须使用array_shift并current获得前2个元素..随着你的数组的增长,这会变得复杂,但array_column会简化这一点
print_r(array_column($arr,"lastname","firstname"));
^ ^
| |
Value Key (I wonder why position is backwards)
Run Code Online (Sandbox Code Playgroud)
Array
(
[Bob] => Tomato
[Larry] => Cucumber
)
Run Code Online (Sandbox Code Playgroud)
最后回到你的问题
What will be returned if a named key doesn't exist?
空数组......来自你的例子
print_r(array_column($arr,"middlename"));
^
|
it would try to check if any of your array has key middle man
Run Code Online (Sandbox Code Playgroud)
Array <------- Otherwise returns empty array
(
)
Run Code Online (Sandbox Code Playgroud)
结论
我曾经使用这样可以在不同的例子loop,array_map,array_reduce并MultipleIterator解释什么array_column努力实现的目标.
你可以看到array_column更加简化,但我建议你稍微使用RFC中的示例,如果你仍然不理解它,这将使你更好地理解它,PHP是一种灵活的语言,你可以随时实现自己的版本
根据:https://wiki.php.net/rfc/array_column
当找不到相应的indexKey时,该值将用一个整数键入,从零开始.
RFC中使用的示例:
$mismatchedColumns = array(
array(
'a' => 'foo',
'b' => 'bar',
'e' => 'baz'
),
array(
'a' => 'qux',
'c' => 'quux',
'd' => 'corge'
),
array(
'a' => 'grault',
'b' => 'garply',
'e' => 'waldo'
),
);
$foo = array_column($mismatchedColumns, 'a', 'b');
Run Code Online (Sandbox Code Playgroud)
结果$foo等于:
Array
(
[bar] => foo
[0] => qux
[garply] => grault
)
Run Code Online (Sandbox Code Playgroud)
从本质上讲,在值一个变为新的数组值,和b成为关键.当原始数组不包含键b时,它会创建一个0索引并使用它.如果有多个密钥不存在,则它们将从0开始递增.
进一步研究它们的例子,它暗示当你无法匹配原始数组中的值时,你根本就得不到数组元素.这意味着如果您在数组中查找单个值并且它不存在,则它将返回一个空数组.
PS我显然从未使用过这个函数,因此大多数都是对RFC的解释.
另外,这个函数被接受包含在PHP中,最初是由Ben Ramsey提出的,最终结果来自38票赞成和6票反对.可以在此处查看邮件列表讨论:http://grokbase.com/t/php/php-internals/126nxxa80p/draft-rfc-array-column-function.另见https://github.com/php/php-src/pull/257