Home > Backend Development > PHP Tutorial > PHP returns a specified column in the array (php5.5.0 default function array_column() is used in PHP<5.5.0)

PHP returns a specified column in the array (php5.5.0 default function array_column() is used in PHP<5.5.0)

WBOY
Release: 2016-07-25 09:12:09
Original
1345 people have browsed it
array_column() returns the column with 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 to do if the php version is less than 5.5.0? Let’s customize one
The following code is taken from onethink
  1. /**
  2. * Returns a specified column 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 uses custom functions
  8. *
  9. * @access public
  10. * @param array $input The multi-dimensional array (or result set) of the array column that needs to be taken out
  11. * @param string $columnKey The column that needs to return the value, It can be a column index of an index array, or a key of a column of an associative array. It can also be NULL, in which case the entire array will be returned (very useful when used with the indexKey parameter to reset the array key)
  12. * @param string $indexKey is the index/key column of the returned array, it can be an integer of the column Index, or 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


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template