PHP Custom Array function Array_column

Source: Internet
Author: User
  1. if (!function_exists (' Array_column ')) {
  2. function Array_column ($input, $columnKey, $indexKey =null) {
  3. $columnKeyIsNumber = (Is_numeric ($columnKey))? True:false;
  4. $indexKeyIsNull = (Is_null ($indexKey))? True:false;
  5. $indexKeyIsNumber = (Is_numeric ($indexKey))? True:false;
  6. $result = Array ();
  7. foreach ((array) $input as $key = + $row) {
  8. if ($columnKeyIsNumber) {
  9. $tmp = Array_slice ($row, $columnKey, 1);
  10. $tmp = (Is_array ($tmp) &&!empty ($tmp))? Current ($TMP): null;
  11. }else{
  12. $tmp = Isset ($row [$columnKey])? $row [$columnKey]: null;
  13. }
  14. if (! $indexKeyIsNull) {
  15. if ($indexKeyIsNumber) {
  16. $key = Array_slice ($row, $indexKey, 1);
  17. $key = (Is_array ($key) &&!empty ($key))? Current ($key): null;
  18. $key = Is_null ($key)? 0: $key;
  19. }else{
  20. $key = Isset ($row [$indexKey])? $row [$indexKey]: 0;
  21. }
  22. }//Bbs.it-home.org
  23. $result [$key] = $tmp;
  24. }
  25. return $result;
  26. }
  27. }
  28. Examples of Use
  29. $records = Array (
  30. Array
  31. ' id ' = 2135,
  32. ' first_name ' = ' John ',
  33. ' last_name ' = ' Doe '
  34. ),
  35. Array
  36. ' id ' = 3245,
  37. ' first_name ' = ' Sally ',
  38. ' last_name ' = ' Smith '
  39. ),
  40. Array
  41. ' id ' = 5342,
  42. ' first_name ' = ' Jane ',
  43. ' last_name ' = ' Jones '
  44. ),
  45. Array
  46. ' id ' = 5623,
  47. ' first_name ' = ' Peter ',
  48. ' last_name ' = ' Doe '
  49. )
  50. );
  51. $firstNames = Array_column ($records, ' first_name ');
  52. Print_r ($firstNames);
  53. /*
  54. Array
  55. (
  56. [0] = John
  57. [1] = Sally
  58. [2] = Jane
  59. [3] = Peter
  60. )
  61. */
  62. $records = Array (
  63. Array (1, ' John ', ' Doe '),
  64. Array (2, ' Sally ', ' Smith '),
  65. Array (3, ' Jane ', ' Jones ')
  66. );
  67. $lastNames = Array_column ($records, 2);
  68. Print_r ($lastNames);
  69. /*
  70. Array
  71. (
  72. [0] = = Doe
  73. [1] = Smith
  74. [2] = Jones
  75. )
  76. */
  77. $mismatchedColumns = Array (
  78. Array
  79. ' A ' = ' foo ',
  80. ' B ' = ' bar ',
  81. ' E ' = ' baz '
  82. ),
  83. Array
  84. ' A ' = ' qux ',
  85. ' C ' = ' Quux ',
  86. ' d ' = ' corge '
  87. ),
  88. Array
  89. ' A ' = ' grault ',
  90. ' B ' = ' garply ',
  91. ' E ' = ' Waldo '
  92. ),
  93. );
  94. $foo = Array_column ($mismatchedColumns, ' A ', ' B ');
  95. Print_r ($foo);
  96. /*
  97. Array
  98. (
  99. [Bar] = foo
  100. [0] = Qux
  101. [Garply] = Grault
  102. )
  103. */
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.