PHP - 使用数字索引获取数组值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6396294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:14:10  来源:igfitidea点击:

PHP - Get array value with a numeric index

phparraysgetindexingnumeric

提问by thom

I have an array like:

我有一个像这样的数组:

$array = array('foo' => 'bar', 33 => 'bin', 'lorem' => 'ipsum');
echo native_function($array, 0); // bar
echo native_function($array, 1); // bin
echo native_function($array, 2); // ipsum

So, this native function would return a value based on a numeric index (second arg), ignoring assoc keys, looking for the real position in array.

因此,这个本机函数将返回一个基于数字索引(第二个参数)的值,忽略关联键,寻找数组中的实际位置。

Are there any native function to do that in PHP or should I write it? Thanks

是否有任何本机函数可以在 PHP 中执行此操作,还是应该编写它?谢谢

回答by genesis

$array = array('foo' => 'bar', 33 => 'bin', 'lorem' => 'ipsum');
$array = array_values($array);
echo $array[0]; //bar
echo $array[1]; //bin
echo $array[2]; //ipsum

回答by John Carter

array_values()will do pretty much what you want:

array_values()几乎可以做你想做的事:

$numeric_indexed_array = array_values($your_array);
// $numeric_indexed_array = array('bar', 'bin', 'ipsum');
print($numeric_indexed_array[0]); // bar

回答by and.ryx

I am proposing my idea about it against any disadvantages array_values( )function, because I think that is not a direct get function. In this way it have to create a copy of the values numerically indexed array and then access. If PHP does not hide a method that automatically translates an integer in the position of the desired element, maybe a slightly better solution might consist of a function that runs the array with a counter until it leads to the desired position, then return the element reached.

我针对任何缺点array_values( )函数提出了我的想法,因为我认为这不是一个直接的 get 函数。通过这种方式,它必须创建数值索引数组的副本,然后访问。如果 PHP 没有隐藏自动将整数转换为所需元素位置的方法,也许更好的解决方案可能包括一个函数,该函数使用计数器运行数组,直到到达所需位置,然后返回到达的元素.

So the work would be optimized for very large array of sizes, since the algorithm would be best performing indices for small, stopping immediately. In the solution highlighted of array_values( ), however, it has to do with a cycle flowing through the whole array, even if, for e.g., I have to access $ array [1].

因此,该工作将针对非常大的数组大小进行优化,因为该算法对于小型索引的性能最佳,并立即停止。array_values( )然而,在突出显示的 解决方案中,它与流经整个数组的循环有关,即使例如,我必须访问 $array [1]。

function array_get_by_index($index, $array) {

    $i=0;
    foreach ($array as $value) {
        if($i==$index) {
            return $value;
        }
        $i++;
    }
    // may be $index exceedes size of $array. In this case NULL is returned.
    return NULL;
}

回答by netcoder

Yes, for scalar values, a combination of implodeand array_slicewill do:

是的,对于标量值,implodearray_slice的组合可以:

$bar = implode(array_slice($array, 0, 1));
$bin = implode(array_slice($array, 1, 1));
$ipsum = implode(array_slice($array, 2, 1));

Or mix it up with array_valuesand list(thanks @nikic) so that it works with all types of values:

或者将它与array_valueslist(感谢@nikic)混合使用,以便它适用于所有类型的值:

list($bar) = array_values(array_slice($array, 0, 1));