PHP returns the specified column in the array (php5.5.0 default function Array_column () in php<5.5.0 application)

Source: Internet
Author: User
Array_column () Returns the column of the specified key name in the array
(PHP 5 >= 5.5.0)
array_column-returns a column specified in the array
PHP Reference Manual: http://www.php.net/manual/zh/function.array-column.php

What if the PHP version is less than 5.5.0? We customize a
The following code is excerpted from Onethink
  1. /**
  2. * Returns a column specified in the array
  3. * http://www.onethink.cn
  4. */application/common/common/function.php
  5. *
  6. * array_column-php 5 >= 5.5.0 default function
  7. * PHP 5 < 5.5.0 use custom functions
  8. *
  9. * @access Public
  10. * @param array $input a multidimensional array (or result set) that requires the array column to be fetched
  11. * @param string $columnKey A column that needs to return a value, which can be the column index of an indexed array, or the key of a column of an associative array. It can also be null, and this will return the entire array (which works well with the Indexkey parameter to reset the array key)
  12. * @param string $indexKey as the column that returns the index/key of the array, which 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. }
Copy Code
    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. ?>
Copy Code
    1. Array
    2. (
    3. [0] = John
    4. [1] = Sally
    5. [2] = Jane
    6. [3] = Peter
    7. )
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.