简体   繁体   English

Arrays:array_shift($arr) 还是 $arr[0]?

[英]Arrays: array_shift($arr) or $arr[0]?

Which one would you use?你会用哪一个?

Basically I only want to get the 1st element from a array, that's it.基本上我只想从数组中获取第一个元素,就是这样。

Well, they do different things.好吧,他们做不同的事情。

  • array_shift($arr) takes the first element out of the array, and gives it to you. array_shift($arr)从数组中取出第一个元素,并将其提供给您。

  • $arr[0] just gives it to you... if the array has numeric keys. $arr[0]只是把它给你......如果数组有数字键。

An alternative that works for associative arrays too is reset($arr) .也适用于关联 arrays 的替代方法是reset($arr) This does move the array's internal pointer, but unless you're using those functions this is unlikely to affect you.这确实会移动数组的内部指针,但除非您使用这些函数,否则这不太可能影响您。

array_shift will actually remove the specified value from the array. array_shift实际上会从数组中删除指定的值。 Do not use it unless you really want to reduce the array!除非你真的想减少数组,否则不要使用它!

See here: http://php.net/manual/en/function.array-shift.php见这里: http://php.net/manual/en/function.array-shift.php

You would use $arr[ 0 ];你会使用$arr[ 0 ]; array_shift removes the first element from the array. array_shift从数组中删除第一个元素。

EDIT编辑


This answer is actually somewhere between incomplete and plain out wrong but, because the comments of the two jon 's I think that it should actually stay up so that others can see that discourse.这个答案实际上介于不完整和明显错误之间,但是,因为两个jon的评论,我认为它实际上应该保持不变,以便其他人可以看到该话语。

The right answer:正确答案:

  • reset is the method to return the first defined index of the array. reset是返回数组的第一个定义索引的方法。 Even in non-associative arrays, this may not be the 0 index.即使在非关联 arrays 中,这也可能不是0索引。
  • array_shift will remove and return the value which is found at reset array_shift将删除并返回reset时找到的值

The OP made the assumption that $arr[0] is the first index is not accurate in that particular context. OP 假设$arr[0]是第一个索引在该特定上下文中是不准确的。

$arr[0] only works if the array as numerical keys. $arr[0]仅在数组作为数字键时才有效。

array_shift removes the element from the array and it modifies the array itself. array_shift从数组中删除元素并修改数组本身。

If you are not sure what the first key is, and you do not want to remove it from the array, you could use:如果您不确定第一个键是什么,并且不想将其从数组中删除,则可以使用:

<?php
foreach($arr $k=>$v){
   $value = $v;
   break;
}

or even better:甚至更好:

<?php
reset($arr);
$value = current($arr);

If you have an associative Array you can also use reset($arr) : It returns the first Element (doesn't remove), and sets the array pointer to this element.如果你有一个关联数组,你也可以使用reset($arr) :它返回第一个元素(不删除),并将数组指针设置为这个元素。

But the fastest way is $arr[0] .但最快的方法是$arr[0]

Do you want to modify the arr array also?你也想修改arr数组吗? array_shift removes the first element of the array and returns it, thus the array has changed. array_shift删除数组的第一个元素并将其返回,因此数组已更改。 $arr[0] merely gives you the first element. $arr[0]只给你第一个元素。

I would use $arr[0] unless I explicitly wanted to modify the array.除非我明确想要修改数组,否则我会使用$arr[0] You may add code later to use the arr array and forget that it was modified.您可以稍后添加代码以使用arr数组并忘记它已被修改。

given what you need, $arr[0] is preferrable, because it's faster.鉴于您的需要, $arr[0] 是可取的,因为它更快。 array_shift is used in other situations. array_shift 用于其他情况。

arrshift is more reliable and will always return the first element in the array, but this also modifies the array by removing that element. arrshift 更可靠,并且总是返回数组中的第一个元素,但这也会通过删除该元素来修改数组。

arr[0] will fail if your array doesn't start at the 0 index, but leaves the array itself alone.如果您的数组不是从 0 索引开始,arr[0] 将失败,而是让数组本身保持不变。

A more convoluted but reliable method is:一种更复杂但更可靠的方法是:

$keys = array_keys($arr);
$first = $arr[$keys[0]];

If you want the first element of an array, use $arr[0] form.如果您想要数组的第一个元素,请使用 $arr[0] 形式。 Advantages - Simplicity, Readability and Maintainability.优点 - 简单、可读性和可维护性。 Keep things straight forward.保持直截了当。

Edit: Use index 0 only if you know that the array has default keys starting from 0.编辑:仅当您知道数组具有从 0 开始的默认键时才使用索引 0。

with array_shif you have two operations:使用 array_shif 你有两个操作:

  1. retrive the firs element检索第一个元素
  2. shift the array移动数组

if you access by index, actually you have only one operation.如果您按索引访问,实际上您只有一个操作。

If you don't want to change the array in question, use $arr[0] (which merely gets the first element), otherwise if you want to remove the first element of $arr from $arr , use array_shift($arr) .如果您不想更改有问题的数组,请使用$arr[0] (仅获取第一个元素),否则如果您想从$arr中删除$arr的第一个元素,请使用array_shift($arr) .

For example:例如:

$arr=array(3,-6,2);
$foo=$arr[0]; //$foo==3 and $arr==array(3,-6,2).
$bar=array_shift($arr); //$bar==3 and $arr==array(-6,2).

ETA: As others have pointed out, be sure that your array isn't an associative array (ie the keys are 0,1,..., (sizeof($arr)-1) ), otherwise this probably won't work. ETA:正如其他人指出的那样,请确保您的数组不是关联数组(即键是 0,1,..., (sizeof($arr)-1) ),否则这可能不起作用.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM