首页 > php教程 > php手册 > 正文

php5.5新数组函数array_column

WBOY
发布: 2016-06-21 08:49:35
原创
756 人浏览过

PHP5.5发布了,其中增加了一个新的数组函数array_column,感觉不错的!但是低版本PHP要使用,得自己实现:
参考地址:https://wiki.php.net/rfc/array_column

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

if(!function_exists('array_column')){

    function array_column($input, $columnKey, $indexKey=null){

        $columnKeyIsNumber      = (is_numeric($columnKey)) ? true : false;

        $indexKeyIsNull         = (is_null($indexKey)) ? true : false;

        $indexKeyIsNumber       = (is_numeric($indexKey)) ? true : false;

        $result                 = array();

        foreach((array)$input as $key=>$row){

            if($columnKeyIsNumber){

                $tmp            = array_slice($row, $columnKey, 1);

                $tmp            = (is_array($tmp) && !empty($tmp)) ? current($tmp) : null;

            }else{

                $tmp            = isset($row[$columnKey]) ? $row[$columnKey] : null;

            }

            if(!$indexKeyIsNull){

                if($indexKeyIsNumber){

                    $key        = array_slice($row, $indexKey, 1);

                    $key        = (is_array($key) && !empty($key)) ? current($key) : null;

                    $key        = is_null($key) ? 0 : $key;

                }else{

                    $key        = isset($row[$indexKey]) ? $row[$indexKey] : 0;

                }

            }

            $result[$key]       = $tmp;

        }

        return $result;

    }

}

 

// 使用例子

$records = array(

    array(

        'id' => 2135,

        'first_name' => 'John',

        'last_name' => 'Doe'

    ),

    array(

        'id' => 3245,

        'first_name' => 'Sally',

        'last_name' => 'Smith'

    ),

    array(

        'id' => 5342,

        'first_name' => 'Jane',

        'last_name' => 'Jones'

    ),

    array(

        'id' => 5623,

        'first_name' => 'Peter',

        'last_name' => 'Doe'

    )

);

$firstNames = array_column($records, 'first_name');

print_r($firstNames);

/*

Array

(

    [0] => John

    [1] => Sally

    [2] => Jane

    [3] => Peter

)

*/

 

$records = array(

    array(1, 'John', 'Doe'),

    array(2, 'Sally', 'Smith'),

    array(3, 'Jane', 'Jones')

);

$lastNames = array_column($records, 2);

print_r($lastNames);

/*

Array

(

    [0] => Doe

    [1] => Smith

    [2] => Jones

)

*/

 

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

print_r($foo);

/*

Array

(

    [bar] => foo

    [0] => qux

    [garply] => grault

)

*/



相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!