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')){

関数 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 = isset($row[$columnKey]) $row[$columnKey] : null;

}

If(!$indexKeyIsNull){

If($indexKeyIsNumber){

$キー

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

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

                }その他{

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

                }

            }

            $result[$key] = $tmp;

        }

        $result を返す;

    }

}

 

// 例を使用します

$records = array(

    配列(

        'id' => 2135、

        '名' => 「ジョン」、

        '姓' => 「ドウ」

    )、

    配列(

        'id' => 3245、

        '名' => 「サリー」、

        '姓' => 「スミス」

    )、

    配列(

        'id' => 5342、

        '名' => 「ジェーン」、

        '姓' => 「ジョーンズ」

    )、

    配列(

        'id' => 5623、

        '名' => 「ピーター」、

        '姓' => 「ドウ」

    )

);

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

print_r($firstNames);

/*

配列

(

    [0] =>ジョン

    [1] =>サリー

    [2] =>ジェーン

    [3] =>ピーター

)

*/

 

$records = array(

    array(1, 'ジョン', 'ドウ'),

    array(2, 'サリー', 'スミス'),

    array(3, 'ジェーン', 'ジョーンズ')

);

$lastNames = array_column($records, 2);

print_r($lastNames);

/*

配列

(

    [0] =>ドウ

    [1] =>スミス

    [2] =>ジョーンズ

)

*/

 

$mismatchedColumns = array(

    配列(

        'a' => 「ふー」、

        'b' => 'バー'、

        'e' => 「バズ」

    )、

    配列(

        'a' => 「クックス」、

        'c' => 「クゥ」、

        'd' => 「コルゲ」

    )、

    配列(

        'a' => 「感謝」、

        'b' => 「ガープライ」、

        'e' => 「ワルド」

    )、

);

$foo = array_column($mismatchedColumns, 'a', 'b');

print_r($foo);

/*

配列

(

    [バー] =>ふー

    [0] =>クックス

    [ガープライ] =>うなり声

)

*/



関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のおすすめ
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!