array_values

(PHP 4, PHP 5, PHP 7, PHP 8)

array_valuesRestituisce tutti i valori di un array

Descrizione

function array_values(array $input): array

array_values() restituisce tutti i valori dell'array input e indicizza numericamente l'array.

Example #1 esempio di array_values()

<?php
$array = array("taglia" => "XL", "colore" => "oro");
print_r(array_values($array));
?>

Il precedente esempio visualizzerà:

Array
(
    [0] => XL
    [1] => oro
)

Vedere anche array_keys().

add a note

User Contributed Notes 6 notes

up
128
biziclop at vipmail dot hu
12 years ago
Remember, array_values() will ignore your beautiful numeric indexes, it will renumber them according tho the 'foreach' ordering:

<?php
$a = array(
 3 => 11,
 1 => 22,
 2 => 33,
);
$a[0] = 44;

print_r( array_values( $a ));
==>
Array(
  [0] => 11
  [1] => 22
  [2] => 33
  [3] => 44
)
?>
up
27
nopy at users dot sourceforge dot net
22 years ago
Just a warning that re-indexing an array by array_values() may cause you to reach the memory limit unexpectly.

For example, if your PHP momory_limits is 8MB,
 and says there's a BIG array $bigArray which allocate 5MB of memory.

Doing this will cause PHP exceeds the momory limits:

<?php
  $bigArray = array_values( $bigArray );
?>

It's because array_values() does not re-index $bigArray directly,
it just re-index it into another array, and assign to itself later.
up
20
abimaelrc
15 years ago
This is another way to get value from a multidimensional array, but for versions of php >= 5.3.x
<?php
/**
 * Get all values from specific key in a multidimensional array
 *
 * @param $key string
 * @param $arr array
 * @return null|string|array
 */
function array_value_recursive($key, array $arr){
    $val = array();
    array_walk_recursive