fork(1) download
  1. <?php
  2.  
  3. /**
  4.  * Ideone usa uma versão antiga do php, mas basta usar a função abaixo
  5.  * que funciona exatamente igual a implementação do php 5.5
  6.  *
  7.  * Basta dar um include da função abaixo no seu projeto
  8.  * https://g...content-available-to-author-only...b.com/ramsey/array_column
  9.  *
  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('array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given', E_USER_WARNING);
  42. return null;
  43. }
  44. if (!is_int($params[1])
  45. && !is_float($params[1])
  46. && !is_string($params[1])
  47. && $params[1] !== null
  48. && !(is_object($params[1]) && method_exists($params[1], '__toString'))
  49. ) {
  50. trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING);
  51. return false;
  52. }
  53. if (isset($params[2])
  54. && !is_int($params[2])
  55. && !is_float($params[2])
  56. && !is_string($params[2])
  57. && !(is_object($params[2]) && method_exists($params[2], '__toString'))
  58. ) {
  59. trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING);
  60. return false;
  61. }
  62. $paramsInput = $params[0];
  63. $paramsColumnKey = ($params[1] !== null) ? (string) $params[1] : null;
  64. $paramsIndexKey = null;
  65. if (isset($params[2])) {
  66. if (is_float($params[2]) || is_int($params[2])) {
  67. $paramsIndexKey = (int) $params[2];
  68. } else {
  69. $paramsIndexKey = (string) $params[2];
  70. }
  71. }
  72. $resultArray = array();
  73. foreach ($paramsInput as $row) {
  74. $key = $value = null;
  75. $keySet = $valueSet = false;
  76. if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) {
  77. $keySet = true;
  78. $key = (string) $row[$paramsIndexKey];
  79. }
  80. if ($paramsColumnKey === null) {
  81. $valueSet = true;
  82. $value = $row;
  83. } elseif (is_array($row) && array_key_exists($paramsColumnKey, $row)) {
  84. $valueSet = true;
  85. $value = $row[$paramsColumnKey];
  86. }
  87. if ($valueSet) {
  88. if ($keySet) {
  89. $resultArray[$key] = $value;
  90. } else {
  91. $resultArray[] = $value;
  92. }
  93. }
  94. }
  95. return $resultArray;
  96. }
  97. }
  98.  
  99. $array = [
  100. ['nome' => 'Luis', 'id' => 31],
  101. ['nome' => 'Rui', 'id' => 42],
  102. ['nome' => 'Joao', 'id' => 113],
  103. ['nome' => 'Joaquim', 'id' => 434],
  104. ['nome' => 'Jorge', 'id' => 503],
  105. ];
  106.  
  107. var_dump(array_column($array, 'nome'));
  108.  
  109. // Se quiser, pode aproveitar uma segunda coluna para usar como key do novo array
  110. var_dump(array_column($array, 'nome', 'id'));
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
array(5) {
  [0]=>
  string(4) "Luis"
  [1]=>
  string(3) "Rui"
  [2]=>
  string(4) "Joao"
  [3]=>
  string(7) "Joaquim"
  [4]=>
  string(5) "Jorge"
}
array(5) {
  [31]=>
  string(4) "Luis"
  [42]=>
  string(3) "Rui"
  [113]=>
  string(4) "Joao"
  [434]=>
  string(7) "Joaquim"
  [503]=>
  string(5) "Jorge"
}