Php returns a specified column in the array (php5.5.0 default function array_column () in php & lt; 5.5.0 applications)

Source: Internet
Author: User
Php returns a specified column in the array (php5.5.0 default function array_column () in php & amp; lt; 5.5.0 application)
Array_column () returns the column with the specified key name in the array.
(PHP 5> = 5.5.0)
Array_column-returns a specified column in the array.
Php Reference: http://www.php.net/manual/zh/function.array-column.php

What if php version is earlier than 5.5.0? We customize
The following code is taken from onethink.

  1. /**
  2. * Returns a specified column in the array.
  3. * Http://www.onethink.cn
  4. */Application/Common/function. php
  5. *
  6. * Array_column-PHP 5> = 5.5.0 default function
  7. * PHP 5 <5.5.0 uses a user-defined function
  8. *
  9. * @ Access public
  10. * @ Param array $ input needs to retrieve the multi-dimensional array (or result set) of the array column)
  11. * @ Param string $ columnKey the column to be returned. it can be the index of the index array or the key of the column associated with the array. It can also be NULL. at this time, the entire array will be returned (it works very well when the indexKey parameter is used to reset the array key)
  12. * @ Param string $ indexKey is the index/key column of the returned array. it can be an integer index of the column or a string key value.
  13. * @ Return array
  14. */
  15. If (! Function_exists ('Array _ column '))
  16. {
  17. Function array_column (array $ input, $ columnKey, $ indexKey = null)
  18. {
  19. $ Result = array ();
  20. If (null ===$ indexKey)
  21. {
  22. If (null ===$ columnKey)
  23. {
  24. $ Result = array_values ($ input );
  25. }
  26. Else
  27. {
  28. Foreach ($ input as $ row)
  29. {
  30. $ Result [] = $ row [$ columnKey];
  31. }
  32. }
  33. }
  34. Else
  35. {
  36. If (null ===$ columnKey)
  37. {
  38. Foreach ($ input as $ row)
  39. {
  40. $ Result [$ row [$ indexKey] = $ row;
  41. }
  42. }
  43. Else
  44. {
  45. Foreach ($ input as $ row)
  46. {
  47. $ Result [$ row [$ indexKey] = $ row [$ columnKey];
  48. }
  49. }
  50. }
  51. Return $ result;
  52. }
  53. }

  1. // Array representing a possible record set returned from a database
  2. $ Records = array (
  3. Array (
  4. 'Id' => 2135,
  5. 'First _ name' => 'John ',
  6. 'Last _ name' => 'Doe ',
  7. ),
  8. Array (
  9. 'Id' => 3245,
  10. 'First _ name' => 'Sally ',
  11. 'Last _ name' => 'Smith ',
  12. ),
  13. Array (
  14. 'Id' => 5342,
  15. 'First _ name' => 'Jane ',
  16. 'Last _ name' => 'Jones ',
  17. ),
  18. Array (
  19. 'Id' => 5623,
  20. 'First _ name' => 'Peter ',
  21. 'Last _ name' => 'Doe ',
  22. )
  23. );
  24. $ First_names = array_column ($ records, 'First _ name ');
  25. Print_r ($ first_names );
  26. ?>

  1. Array
  2. (
  3. [0] => John
  4. [1] => Sally
  5. [2] => Jane
  6. [3] => Peter
  7. )

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.