array_column.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * This file is part of the array_column library
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @copyright Copyright (c) Ben Ramsey (http://benramsey.com)
  9. * @license http://opensource.org/licenses/MIT MIT
  10. */
  11. if (!function_exists('array_column')) {
  12. /**
  13. * Returns the values from a single column of the input array, identified by
  14. * the $columnKey.
  15. *
  16. * Optionally, you may provide an $indexKey to index the values in the returned
  17. * array by the values from the $indexKey column in the input array.
  18. *
  19. * @param array $input A multi-dimensional array (record set) from which to pull
  20. * a column of values.
  21. * @param mixed $columnKey The column of values to return. This value may be the
  22. * integer key of the column you wish to retrieve, or it
  23. * may be the string key name for an associative array.
  24. * @param mixed $indexKey (Optional.) The column to use as the index/keys for
  25. * the returned array. This value may be the integer key
  26. * of the column, or it may be the string key name.
  27. * @return array
  28. */
  29. function array_column($input = null, $columnKey = null, $indexKey = null)
  30. {
  31. // Using func_get_args() in order to check for proper number of
  32. // parameters and trigger errors exactly as the built-in array_column()
  33. // does in PHP 5.5.
  34. $argc = func_num_args();
  35. $params = func_get_args();
  36. if ($argc < 2) {
  37. trigger_error("array_column() expects at least 2 parameters, {$argc} given", E_USER_WARNING);
  38. return null;
  39. }
  40. if (!is_array($params[0])) {
  41. trigger_error(
  42. 'array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given',
  43. E_USER_WARNING
  44. );
  45. return null;
  46. }
  47. if (!is_int($params[1])
  48. && !is_float($params[1])
  49. && !is_string($params[1])
  50. && $params[1] !== null
  51. && !(is_object($params[1]) && method_exists($params[1], '__toString'))
  52. ) {
  53. trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING);
  54. return false;
  55. }
  56. if (isset($params[2])
  57. && !is_int($params[2])
  58. && !is_float($params[2])
  59. && !is_string($params[2])
  60. && !(is_object($params[2]) && method_exists($params[2], '__toString'))
  61. ) {
  62. trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING);
  63. return false;
  64. }
  65. $paramsInput = $params[0];
  66. $paramsColumnKey = ($params[1] !== null) ? (string) $params[1] : null;
  67. $paramsIndexKey = null;
  68. if (isset($params[2])) {
  69. if (is_float($params[2]) || is_int($params[2])) {
  70. $paramsIndexKey = (int) $params[2];
  71. } else {
  72. $paramsIndexKey = (string) $params[2];
  73. }
  74. }
  75. $resultArray = array();
  76. foreach ($paramsInput as $row) {
  77. $key = $value = null;
  78. $keySet = $valueSet = false;
  79. if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) {
  80. $keySet = true;
  81. $key = (string) $row[$paramsIndexKey];
  82. }
  83. if ($paramsColumnKey === null) {
  84. $valueSet = true;
  85. $value = $row;
  86. } elseif (is_array($row) && array_key_exists($paramsColumnKey, $row)) {
  87. $valueSet = true;
  88. $value = $row[$paramsColumnKey];
  89. }
  90. if ($valueSet) {
  91. if ($keySet) {
  92. $resultArray[$key] = $value;
  93. } else {
  94. $resultArray[] = $value;
  95. }
  96. }
  97. }
  98. return $resultArray;
  99. }
  100. }