array_values
(PHP 4, PHP 5)
array_values — Выбрать все значения массива
Описание
array array_values
( array $input
)
array_values() возвращает индексный массив, содержащий все значения массива input .
Пример #1 Пример использованияarray_values()
<?php
$array = array("size" => "XL", "color" => "gold");
print_r(array_values($array));
?>
Выведет:
Array ( [0] => XL [1] => gold )
См. также array_keys().
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения, относящиеся к переменным и типам
- Массивы
- array_change_key_case
- array_chunk
- array_column
- array_combine
- array_count_values
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_diff
- array_fill_keys
- array_fill
- array_filter
- array_flip
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_intersect
- array_key_exists
- array_keys
- array_map
- array_merge_recursive
- array_merge
- array_multisort
- array_pad
- array_pop
- array_product
- array_push
- array_rand
- array_reduce
- array_replace_recursive
- array_replace
- array_reverse
- array_search
- array_shift
- array_slice
- array_splice
- array_sum
- array_udiff_assoc
- array_udiff_uassoc
- array_udiff
- array_uintersect_assoc
- array_uintersect_uassoc
- array_uintersect
- array_unique
- array_unshift
- array_values
- array_walk_recursive
- array_walk
- array
- arsort
- asort
- compact
- count
- current
- each
- end
- extract
- in_array
- key_exists
- key
- krsort
- ksort
- list
- natcasesort
- natsort
- next
- pos
- prev
- range
- reset
- rsort
- shuffle
- sizeof
- sort
- uasort
- uksort
- usort
Коментарии
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.
<?php
/**
flatten an arbitrarily deep multidimensional array
into a list of its scalar values
(may be inefficient for large structures)
(will infinite recurse on self-referential structures)
(could be extended to handle objects)
*/
function array_values_recursive($ary)
{
$lst = array();
foreach( array_keys($ary) as $k ){
$v = $ary[$k];
if (is_scalar($v)) {
$lst[] = $v;
} elseif (is_array($v)) {
$lst = array_merge( $lst,
array_values_recursive($v)
);
}
}
return $lst;
}
?>
code till dawn! -mark meves!
Most of the array_flatten functions don't allow preservation of keys. Mine allows preserve, don't preserve, and preserve only strings (default).
<?
// recursively reduces deep arrays to single-dimensional arrays
// $preserve_keys: (0=>never, 1=>strings, 2=>always)
function array_flatten($array, $preserve_keys = 1, &$newArray = Array()) {
foreach ($array as $key => $child) {
if (is_array($child)) {
$newArray =& array_flatten($child, $preserve_keys, $newArray);
} elseif ($preserve_keys + is_string($key) > 1) {
$newArray[$key] = $child;
} else {
$newArray[] = $child;
}
}
return $newArray;
}
// Tests
$array = Array(
'A' => Array(
1 => 'foo',
2 => Array(
'a' => 'bar'
)
),
'B' => 'baz'
);
echo 'var_dump($array);'."\n";
var_dump($array);
echo 'var_dump(array_flatten($array, 0));'."\n";
var_dump(array_flatten($array, 0));
echo 'var_dump(array_flatten($array, 1));'."\n";
var_dump(array_flatten($array, 1));
echo 'var_dump(array_flatten($array, 2));'."\n";
var_dump(array_flatten($array, 2));
?>
If you are looking for a way to count the total number of times a specific value appears in array, use this function:
<?php
function array_value_count ($match, $array)
{
$count = 0;
foreach ($array as $key => $value)
{
if ($value == $match)
{
$count++;
}
}
return $count;
}
?>
This should really be a native function of PHP.
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($arr, function($v, $k) use($key, &$val){
if($k == $key) array_push($val, $v);
});
return count($val) > 1 ? $val : array_pop($val);
}
$arr = array(
'foo' => 'foo',
'bar' => array(
'baz' => 'baz',
'candy' => 'candy',
'vegetable' => array(
'carrot' => 'carrot',
)
),
'vegetable' => array(
'carrot' => 'carrot2',
),
'fruits' => 'fruits',
);
var_dump(array_value_recursive('carrot', $arr)); // array(2) { [0]=> string(6) "carrot" [1]=> string(7) "carrot2" }
var_dump(array_value_recursive('apple', $arr)); // null
var_dump(array_value_recursive('baz', $arr)); // string(3) "baz"
var_dump(array_value_recursive('candy', $arr)); // string(5) "candy"
var_dump(array_value_recursive('pear', $arr)); // null
?>
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
)
?>