Php custom array function array_column

Source: Internet
Author: User
Php custom array function array_column

  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. // Example
  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' => 'graresult ',
  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. */

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.