airdge
2018-05-15 11:13:58 +08:00
用 array_column 提取 foo 作为新数组,新数组去重,获取相应键值就可以了
<?php
class str
{
public static $data = [
[
'foo' => 'bar',
'x' => 'foo',
],
[
'foo' => 'bar1',
'x' => 'foo1',
],
[
'foo' => 'bar',
'x' => 'foo2',
],
[
'foo' => 'bar2',
'x' => 'foo2',
],
[
'foo' => 'bar2',
'x' => 'foo1',
],
[
'foo' => 'bar3',
'x' => 'foo4',
],
];
public static function a() {
return array_map("self::b", array_keys(array_unique(array_column(self::$data, 'foo'))));
}
public static function b($a) {
return self::$data[$a];
}
}
print_r(str::a());
Array
(
[0] => Array
(
[foo] => bar
[x] => foo
)
[1] => Array
(
[foo] => bar1
[x] => foo1
)
[2] => Array
(
[foo] => bar2
[x] => foo2
)
[3] => Array
(
[foo] => bar3
[x] => foo4
)
)