array_values() - 返回数组中所有的值 - php 数组函数
array_values()
(PHP 4, PHP 5, PHP 7)
返回数组中所有的值
说明
array_values(array $array): arrayarray_values()返回$input数组中所有的值并给其建立数字索引。
参数
$array数组。
返回值
返回含所有值的索引数组。
范例
Example #1array_values()例子
以上例程会输出:
Array ( [0] => XL [1] => gold )
参见
array_keys()
返回数组中部分的或所有的键名array_combine()
创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值
Remember, array_values() will ignore your beautiful numeric indexes, it will renumber them according tho the 'foreach' ordering:
This is another way to get value from a multidimensional array, but for versions of php >= 5.3.x
Most of the array_flatten functions don't allow preservation of keys. Mine allows preserve, don't preserve, and preserve only strings (default).
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: It's because array_values() does not re-index $bigArray directly, it just re-index it into another array, and assign to itself later.
code till dawn! -mark meves!
If you are looking for a way to count the total number of times a specific value appears in array, use this function: This should really be a native function of PHP.
I needed a function that recursively went into each level of the array to order (only the indexed) arrays... and NOT flatten the whole thing. Solution:
This function is an improved and faster version of the one posted by
extract all values from a multi dimesnsional array or a nexted json object function array_keys_multi($array,&$vals) { foreach ($array as $key => $value) { if (is_array($value)) { array_keys_multi($value,$vals); }else{ $vals[] = $value; } } return $vals; }
Non-recursive simplest array_flatten.
same array_flatten function, compressed and preserving keys. function array_flatten($a,$f=array()){ if(!$a||!is_array($a))return ''; foreach($a as $k=>$v){ if(is_array($v))$f=array_flatten($v,$f); else $f[$k]=$v; } return $f; }
/** * Return the new array from the offset index */ function array_values_from($array, $offset_index = 0) { if (!is_array($array)) return null; $index = (int)$offset_index; foreach($array as $i => $value) $array_return[$index++] = $value; return $array_return; }
A modification of wellandpower at hotmail.com's function to perform array_values recursively. This version will only re-index numeric keys, leaving associative array indexes alone.
wil result: Array ( [0] => XL [1] => gold [2] => [3] => gold [4] => )
Please note that 'wellandpower at hotmail.com's recursive merge doesn't work. Here's the fixed version:
A comment on array_merge mentioned that array_splice is faster than array_merge for inserting values. This may be the case, but if your goal is instead to reindex a numeric array, array_values() is the function of choice. Performing the following functions in a 100,000-iteration loop gave me the following times: ($b is a 3-element array) array_splice($b, count($b)) => 0.410652 $b = array_splice($b, 0) => 0.272513 array_splice($b, 3) => 0.26529 $b = array_merge($b) => 0.233582 $b = array_values($b) => 0.151298
The function here flatterns an entire array and was not the behaviour I expected from a function of this name. I expected the function to flattern every sub array so that all the values were aligned and it would return an array with the same dimensions as the imput array, but as per array_values() adjusting the keys rater than removing them. In order to do this, you will want this function: function array_values_recursive($array) { $temp = array(); foreach ($array as $value) { if(is_array($value)) { $temp[] = array_values_recursive($value); } else { $temp[] = $value; } } return $temp; } Hopefully this will assist.
Remember, that the following way of fetching data from a mySql-Table will do exactly the thing as carl described before: An array, which data may be accessed both by numerical and DB-ID-based Indexes: Hope I haven't misunderstood anything here.. :)
Indeed you can, and that's what's so great about it. I have, for instance, a function that returns the results of a database query as an array. I want to keep the order that the entries were returned in, but at the same time I want to be able to access them _either_ by the position _or_ by some other index (such as some sort of ID in the database, gotten from elsewhere). In this case, I can make the function return an array from id to [array of values], and by a simple call to array_values() this is transformed into an array indexed from 0 to count() 1. Useful.
Note that in a multidimensional array, each element may be identified by a _sequence_ of keys, i.e. the keys that lead towards that element. Thus "preserving keys" may have different interpretations. Ivan's function for example creates a two-dimensional array preserving the last two keys. Other functions below create a one-dimensional array preserving the last key. For completeness, I will add a function that merges the key sequence by a given separator and a function that preserves the last n keys, where n is arbitrary.
@Yassin Ezbakhe When we have to flatten multidimensional array of strings or numbers this method could be much faster. Inconvenience of this method is, that its speed depends on size of strings/numbers, which array contains - bigger strings, lower efficiency. Conclusion: Use this method for small amount of data in arrays (less than 500B per element in my case) which have many dimensions, in other case, use Yassin Ezbakhe method.
Good function, if you want to acces associative array element by position:
In case you want to replace all keys in multiarrays by integers starting at 0, the following function might help.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)