<?php
/**
* Ideone usa uma versão antiga do php, mas basta usar a função abaixo
* que funciona exatamente igual a implementação do php 5.5
*
* Basta dar um include da função abaixo no seu projeto
* https://g...content-available-to-author-only...b.com/ramsey/array_column
*
*/
/**
* Returns the values from a single column of the input array, identified by
* the $columnKey.
*
* Optionally, you may provide an $indexKey to index the values in the returned
* array by the values from the $indexKey column in the input array.
*
* @param array $input A multi-dimensional array (record set) from which to pull
* a column of values.
* @param mixed $columnKey The column of values to return. This value may be the
* integer key of the column you wish to retrieve, or it
* may be the string key name for an associative array.
* @param mixed $indexKey (Optional.) The column to use as the index/keys for
* the returned array. This value may be the integer key
* of the column, or it may be the string key name.
* @return array
*/
function array_column($input = null, $columnKey = null, $indexKey = null)
{
// Using func_get_args() in order to check for proper number of
// parameters and trigger errors exactly as the built-in array_column()
// does in PHP 5.5.
if ($argc < 2) {
trigger_error("array_column() expects at least 2 parameters, {$argc} given", E_USER_WARNING); return null;
}
trigger_error('array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given', E_USER_WARNING); return null;
}
&& $params[1] !== null
) {
trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING); return false;
}
) {
trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING); return false;
}
$paramsInput = $params[0];
$paramsColumnKey = ($params[1] !== null) ? (string) $params[1] : null;
$paramsIndexKey = null;
$paramsIndexKey = (int) $params[2];
} else {
$paramsIndexKey = (string) $params[2];
}
}
foreach ($paramsInput as $row) {
$key = $value = null;
$keySet = $valueSet = false;
$keySet = true;
$key = (string) $row[$paramsIndexKey];
}
if ($paramsColumnKey === null) {
$valueSet = true;
$value = $row;
$valueSet = true;
$value = $row[$paramsColumnKey];
}
if ($valueSet) {
if ($keySet) {
$resultArray[$key] = $value;
} else {
$resultArray[] = $value;
}
}
}
return $resultArray;
}
}
$array = [
['nome' => 'Luis', 'id' => 31],
['nome' => 'Rui', 'id' => 42],
['nome' => 'Joao', 'id' => 113],
['nome' => 'Joaquim', 'id' => 434],
['nome' => 'Jorge', 'id' => 503],
];
// Se quiser, pode aproveitar uma segunda coluna para usar como key do novo array
var_dump(array_column
($array, 'nome', 'id'));
PD9waHAKCi8qKgogKiBJZGVvbmUgdXNhIHVtYSB2ZXJzw6NvIGFudGlnYSBkbyBwaHAsIG1hcyBiYXN0YSB1c2FyIGEgZnVuw6fDo28gYWJhaXhvCiAqIHF1ZSBmdW5jaW9uYSBleGF0YW1lbnRlIGlndWFsIGEgaW1wbGVtZW50YcOnw6NvIGRvIHBocCA1LjUKICogCiAqIEJhc3RhIGRhciB1bSBpbmNsdWRlIGRhIGZ1bsOnw6NvIGFiYWl4byBubyBzZXUgcHJvamV0bwogKiBodHRwczovL2cuLi5jb250ZW50LWF2YWlsYWJsZS10by1hdXRob3Itb25seS4uLmIuY29tL3JhbXNleS9hcnJheV9jb2x1bW4gCiAqIAogKi8KaWYgKCFmdW5jdGlvbl9leGlzdHMoJ2FycmF5X2NvbHVtbicpKSB7CiAgICAvKioKICAgICAqIFJldHVybnMgdGhlIHZhbHVlcyBmcm9tIGEgc2luZ2xlIGNvbHVtbiBvZiB0aGUgaW5wdXQgYXJyYXksIGlkZW50aWZpZWQgYnkKICAgICAqIHRoZSAkY29sdW1uS2V5LgogICAgICoKICAgICAqIE9wdGlvbmFsbHksIHlvdSBtYXkgcHJvdmlkZSBhbiAkaW5kZXhLZXkgdG8gaW5kZXggdGhlIHZhbHVlcyBpbiB0aGUgcmV0dXJuZWQKICAgICAqIGFycmF5IGJ5IHRoZSB2YWx1ZXMgZnJvbSB0aGUgJGluZGV4S2V5IGNvbHVtbiBpbiB0aGUgaW5wdXQgYXJyYXkuCiAgICAgKgogICAgICogQHBhcmFtIGFycmF5ICRpbnB1dCBBIG11bHRpLWRpbWVuc2lvbmFsIGFycmF5IChyZWNvcmQgc2V0KSBmcm9tIHdoaWNoIHRvIHB1bGwKICAgICAqICAgICAgICAgICAgICAgICAgICAgYSBjb2x1bW4gb2YgdmFsdWVzLgogICAgICogQHBh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mdzKCk7CiAgICAgICAgaWYgKCRhcmdjIDwgMikgewogICAgICAgICAgICB0cmlnZ2VyX2Vycm9yKCJhcnJheV9jb2x1bW4oKSBleHBlY3RzIGF0IGxlYXN0IDIgcGFyYW1ldGVycywgeyRhcmdjfSBnaXZlbiIsIEVfVVNFUl9XQVJOSU5HKTsKICAgICAgICAgICAgcmV0dXJuIG51bGw7CiAgICAgICAgfQogICAgICAgIGlmICghaXNfYXJyYXkoJHBhcmFtc1swXSkpIHsKICAgICAgICAgICAgdHJpZ2dlcl9lcnJvcignYXJyYXlfY29sdW1uKCkgZXhwZWN0cyBwYXJhbWV0ZXIgMSB0byBiZSBhcnJheSwgJyAuIGdldHR5cGUoJHBhcmFtc1swXSkgLiAnIGdpdmVuJywgRV9VU0VSX1dBUk5JTkcpOwogICAgICAgICAgICByZXR1cm4gbnVsbDsKICAgICAgICB9CiAgICAgICAgaWYgKCFpc19pbnQoJHBhcmFtc1sxXSkKICAgICAgICAgICAgJiYgIWlzX2Zsb2F0KCRwYXJhbXNbMV0pCiAgICAgICAgICAgICYmICFpc19zdHJpbmcoJHBhcmFtc1sxXSkKICAgICAgICAgICAgJiYgJHBhcmFtc1sxXSAhPT0gbnVsbAogICAgICAgICAgICAmJiAhKGlzX29iamVjdCgkcGFyYW1zWzFdKSAmJiBtZXRob2RfZXhpc3RzKCRwYXJhbXNbMV0sICdfX3RvU3RyaW5nJykpCiAgICAgICAgKSB7CiAgICAgICAgICAgIHRyaWdnZXJfZXJyb3IoJ2FycmF5X2NvbHVtbigpOiBUaGUgY29sdW1uIGtleSBzaG91bGQgYmUgZWl0aGVyIGEgc3RyaW5nIG9yIGFuIGludGVnZXInLCBFX1VTRVJfV0FSTklORyk7CiAgICAgICAgICAgIHJldHVybiBmYWxzZTsKICAgICAgICB9CiAgICAgICAgaWYgKGlzc2V0KCRwYXJhbXNbMl0pCiAgICAgICAgICAgICYmICFpc19pbnQoJHBhcmFtc1syXSkKICAgICAgICAgICAgJiYgIWlzX2Zsb2F0KCRwYXJhbXNbMl0pCiAgICAgICAgICAgICYmICFpc19zdHJpbmcoJHBhcmFtc1syXSkKICAgICAgICAgICAgJiYgIShpc19vYmplY3QoJHBhcmFtc1syXSkgJiYgbWV0aG9kX2V4aXN0cygkcGFyYW1zWzJdLCAnX190b1N0cmluZycpKQogICAgICAgICkgewogICAgICAgICAgICB0cmlnZ2VyX2Vycm9yKCdhcnJheV9jb2x1bW4oKTogVGhlIGluZGV4IGtleSBzaG91bGQgYmUgZWl0aGVyIGEgc3RyaW5nIG9yIGFuIGludGVnZXInLCBFX1VTRVJfV0FSTklORyk7CiAgICAgICAgICAgIHJldHVybiBmYWxzZTsKICAgICAgICB9CiAgICAgICAgJHBhcmFtc0lucHV0ID0gJHBhcmFtc1swXTsKICAgICAgICAkcGFyYW1zQ29sdW1uS2V5ID0gKCRwYXJhbXNbMV0gIT09IG51bGwpID8gKHN0cmluZykgJHBhcmFtc1sxXSA6IG51bGw7CiAgICAgICAgJHBhcmFtc0luZGV4S2V5ID0gbnVsbDsKICAgICAgICBpZiAoaXNzZXQoJHBhcmFtc1syXSkpIHsKICAgICAgICAgICAgaWYgKGlzX2Zsb2F0KCRwYXJhbXNbMl0pIHx8IGlzX2ludCgkcGFyYW1zWzJdKSkgewogICAgICAgICAgICAgICAgJHBhcmFtc0luZGV4S2V5ID0gKGludCkgJHBhcmFtc1syXTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICRwYXJhbXNJbmRleEtleSA9IChzdHJpbmcpICRwYXJhbXNbMl07CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgJHJlc3VsdEFycmF5ID0gYXJyYXkoKTsKICAgICAgICBmb3JlYWNoICgkcGFyYW1zSW5wdXQgYXMgJHJvdykgewogICAgICAgICAgICAka2V5ID0gJHZhbHVlID0gbnVsbDsKICAgICAgICAgICAgJGtleVNldCA9ICR2YWx1ZVNldCA9IGZhbHNlOwogICAgICAgICAgICBpZiAoJHBhcmFtc0luZGV4S2V5ICE9PSBudWxsICYmIGFycmF5X2tleV9leGlzdHMoJHBhcmFtc0luZGV4S2V5LCAkcm93KSkgewogICAgICAgICAgICAgICAgJGtleVNldCA9IHRydWU7CiAgICAgICAgICAgICAgICAka2V5ID0gKHN0cmluZykgJHJvd1skcGFyYW1zSW5kZXhLZXldOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmICgkcGFyYW1zQ29sdW1uS2V5ID09PSBudWxsKSB7CiAgICAgICAgICAgICAgICAkdmFsdWVTZXQgPSB0cnVlOwogICAgICAgICAgICAgICAgJHZhbHVlID0gJHJvdzsKICAgICAgICAgICAgfSBlbHNlaWYgKGlzX2FycmF5KCRyb3cpICYmIGFycmF5X2tleV9leGlzdHMoJHBhcmFtc0NvbHVtbktleSwgJHJvdykpIHsKICAgICAgICAgICAgICAgICR2YWx1ZVNldCA9IHRydWU7CiAgICAgICAgICAgICAgICAkdmFsdWUgPSAkcm93WyRwYXJhbXNDb2x1bW5LZXldOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmICgkdmFsdWVTZXQpIHsKICAgICAgICAgICAgICAgIGlmICgka2V5U2V0KSB7CiAgICAgICAgICAgICAgICAgICAgJHJlc3VsdEFycmF5WyRrZXldID0gJHZhbHVlOwogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAkcmVzdWx0QXJyYXlbXSA9ICR2YWx1ZTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gJHJlc3VsdEFycmF5OwogICAgfQp9CgogICAgJGFycmF5ID0gWwogICAgCVsnbm9tZScgPT4gJ0x1aXMnLCAnaWQnID0+IDMxXSwgCiAgICAJWydub21lJyA9PiAnUnVpJywgJ2lkJyA9PiA0Ml0sCiAgICAJWydub21lJyA9PiAnSm9hbycsICdpZCcgPT4gMTEzXSwKICAgIAlbJ25vbWUnID0+ICdKb2FxdWltJywgJ2lkJyA9PiA0MzRdLAogICAgCVsnbm9tZScgPT4gJ0pvcmdlJywgJ2lkJyA9PiA1MDNdLAogICAgXTsKCiAgICB2YXJfZHVtcChhcnJheV9jb2x1bW4oJGFycmF5LCAnbm9tZScpKTsKICAgIAogICAgLy8gU2UgcXVpc2VyLCBwb2RlIGFwcm92ZWl0YXIgdW1hIHNlZ3VuZGEgY29sdW5hIHBhcmEgdXNhciBjb21vIGtleSBkbyBub3ZvIGFycmF5CiAgICB2YXJfZHVtcChhcnJheV9jb2x1bW4oJGFycmF5LCAnbm9tZScsICdpZCcpKTs=