How can I get the element before the last element from an array in PHP5 ?
如何在PHP5中的数组中获得最后一个元素之前的元素?
7 个解决方案
#1
60
$array[count($array)-2]
It should be a numerically indexed array (from zero). You should have at least 2 elements for this to work. (obviously)
它应该是一个数值索引的数组(从零开始)。至少要有两个元素才能让它工作。(很明显)
#2
112
This will work even on this array:
这在这个数组中也适用:
$array[0] = "hello";
$array[5] = "how";
$array[9] = "are";
end($array);
echo prev($array); // will print "how"
The other solutions using count() are assuming that the indexes of your array go in order; by using end and prev to move the array pointer, you get the actual values. Try using the count() method on the array above and it will fail.
使用count()的其他解决方案假设数组的索引是有序的;通过使用end和prev移动数组指针,您可以得到实际的值。尝试在上面的数组上使用count()方法,它将失败。
#3
15
array_slice takes a negative offset as the second argument. This will give you a single item array containing the second last item:
array_slice接受一个负偏移量作为第二个参数。这将给您一个包含第二项的单个项目数组:
$arr = array(1,2,3,4,5,6);
array_slice($arr, -2, 1);
If you just want the single value on it's own you have several options. If you don't mind using an intermediate variable you can then just get the first value with [0] or call array_pop or array_shift, they both need a variable passed by reference or you'll get warnings in strict mode.
如果你只是想要单独的值,你有几个选项。如果您不介意使用中间变量,那么您可以使用[0]获取第一个值,或者调用array_pop或array_shift,它们都需要通过引用传递一个变量,否则将在严格模式下得到警告。
Or you could just use array_sum or array_product, which is a bit hacky but works fine for single item arrays.
或者你也可以使用array_sum或者array_product,这有点繁琐,但对于单个项目数组来说很好用。
#4
12
As for me pretty tiny solution
对我来说,这是一个很小的解决方案
end($array);
echo prev($array);
#5
6
A method that will work for both associative array and numeric array is to use array_pop()
to pop the element off the end of array.
对于关联数组和数字数组都有效的方法是使用array_pop()从数组末尾弹出元素。
$last = array_pop($array);
$second_last = array_pop($array);
// put back the last
array_push($array, $last);
#6
2
// Indexed based array
$test = array('a','b','c','d','e');
$count = count($test);
print $test[$count-2];
// Associative Array
$months = array(
'jan'=>'January',
'feb' => 'february',
'mar' => 'March',
'apr' => 'April'
);
$keys = array_keys($months);
$count = count($keys);
print $keys[$count-2];
#7
2
All arrays have an "internal array pointer" which points to the current array element, PHP has several functions which allow you to navigate through the array and view the current elements key and value.
所有数组都有一个指向当前数组元素的“内部数组指针”,PHP有几个函数允许您在数组中导航并查看当前元素键和值。
-
end()
- Set the internal pointer of an array to its last element - end()—将数组的内部指针设置为其最后一个元素
-
reset()
- Set the internal pointer of an array to its first element - reset()——将数组的内部指针设置为第一个元素
-
prev()
- Rewind the internal array pointer - prev() -返回内部数组指针。
-
next()
- Advance the internal array pointer of an array - next()——提前一个数组的内部数组指针
-
current()
- Return the current element in an array - current()——返回数组中的当前元素
-
key()
- Fetch a key from an array - key() -从数组中获取一个键
-
each()
- Return the current key and value pair from an array and advance the array cursor - each()——从数组返回当前键和值对,并推进数组游标
These functions work whether the array is empty, sequential or associative and as an array has not been specified in the example i've assumed this must work with any array.
无论数组是空的、顺序的还是关联的,这些函数都可以工作,而且作为一个数组,在我假设的示例中还没有指定,它必须适用于任何数组。
$array = array(
'before_last' => false,
'last' => false,
);
end($array); /*
- set pointer to last element -> $array['last']
- return new current element value if it exists, -> false
- else return FALSE
*/
prev($array); /*
- set pointer one place before current pointer -> $array['before_last']
- return new current element value if it exists, -> false
- else return FALSE
*/
if(!is_null(key($array)){ /*
- return current element key if it exists -> "before_last"
- else return NULL
*/
$before_last_element_value = current($array); /*
- return current element value if it exists, -> false
- else return FALSE
*/
}
As you can see the expected result (false
) and the result for a nonexistent element is the same (FALSE
) so you cannot check whether an element exists using the returned element value, an element key is different.
正如您可以看到预期的结果(false)和不存在的元素的结果是相同的(false),因此不能使用返回的元素值检查元素是否存在,因此元素键是不同的。
The key can either be an integer or a string. The value can be of any
type
. source键可以是整数,也可以是字符串。值可以是任何类型。源
The key()
returns the value of the current key if the element exists otherwise it will return NULL. A valid key can never be NULL so if null is returned we can determine that the element does not exist.
key()返回当前键的值,如果元素存在,则返回NULL。一个有效的键永远不会为空,所以如果返回NULL,我们可以确定元素不存在。
#1
60
$array[count($array)-2]
It should be a numerically indexed array (from zero). You should have at least 2 elements for this to work. (obviously)
它应该是一个数值索引的数组(从零开始)。至少要有两个元素才能让它工作。(很明显)
#2
112
This will work even on this array:
这在这个数组中也适用:
$array[0] = "hello";
$array[5] = "how";
$array[9] = "are";
end($array);
echo prev($array); // will print "how"
The other solutions using count() are assuming that the indexes of your array go in order; by using end and prev to move the array pointer, you get the actual values. Try using the count() method on the array above and it will fail.
使用count()的其他解决方案假设数组的索引是有序的;通过使用end和prev移动数组指针,您可以得到实际的值。尝试在上面的数组上使用count()方法,它将失败。
#3
15
array_slice takes a negative offset as the second argument. This will give you a single item array containing the second last item:
array_slice接受一个负偏移量作为第二个参数。这将给您一个包含第二项的单个项目数组:
$arr = array(1,2,3,4,5,6);
array_slice($arr, -2, 1);
If you just want the single value on it's own you have several options. If you don't mind using an intermediate variable you can then just get the first value with [0] or call array_pop or array_shift, they both need a variable passed by reference or you'll get warnings in strict mode.
如果你只是想要单独的值,你有几个选项。如果您不介意使用中间变量,那么您可以使用[0]获取第一个值,或者调用array_pop或array_shift,它们都需要通过引用传递一个变量,否则将在严格模式下得到警告。
Or you could just use array_sum or array_product, which is a bit hacky but works fine for single item arrays.
或者你也可以使用array_sum或者array_product,这有点繁琐,但对于单个项目数组来说很好用。
#4
12
As for me pretty tiny solution
对我来说,这是一个很小的解决方案
end($array);
echo prev($array);
#5
6
A method that will work for both associative array and numeric array is to use array_pop()
to pop the element off the end of array.
对于关联数组和数字数组都有效的方法是使用array_pop()从数组末尾弹出元素。
$last = array_pop($array);
$second_last = array_pop($array);
// put back the last
array_push($array, $last);
#6
2
// Indexed based array
$test = array('a','b','c','d','e');
$count = count($test);
print $test[$count-2];
// Associative Array
$months = array(
'jan'=>'January',
'feb' => 'february',
'mar' => 'March',
'apr' => 'April'
);
$keys = array_keys($months);
$count = count($keys);
print $keys[$count-2];
#7
2
All arrays have an "internal array pointer" which points to the current array element, PHP has several functions which allow you to navigate through the array and view the current elements key and value.
所有数组都有一个指向当前数组元素的“内部数组指针”,PHP有几个函数允许您在数组中导航并查看当前元素键和值。
-
end()
- Set the internal pointer of an array to its last element - end()—将数组的内部指针设置为其最后一个元素
-
reset()
- Set the internal pointer of an array to its first element - reset()——将数组的内部指针设置为第一个元素
-
prev()
- Rewind the internal array pointer - prev() -返回内部数组指针。
-
next()
- Advance the internal array pointer of an array - next()——提前一个数组的内部数组指针
-
current()
- Return the current element in an array - current()——返回数组中的当前元素
-
key()
- Fetch a key from an array - key() -从数组中获取一个键
-
each()
- Return the current key and value pair from an array and advance the array cursor - each()——从数组返回当前键和值对,并推进数组游标
These functions work whether the array is empty, sequential or associative and as an array has not been specified in the example i've assumed this must work with any array.
无论数组是空的、顺序的还是关联的,这些函数都可以工作,而且作为一个数组,在我假设的示例中还没有指定,它必须适用于任何数组。
$array = array(
'before_last' => false,
'last' => false,
);
end($array); /*
- set pointer to last element -> $array['last']
- return new current element value if it exists, -> false
- else return FALSE
*/
prev($array); /*
- set pointer one place before current pointer -> $array['before_last']
- return new current element value if it exists, -> false
- else return FALSE
*/
if(!is_null(key($array)){ /*
- return current element key if it exists -> "before_last"
- else return NULL
*/
$before_last_element_value = current($array); /*
- return current element value if it exists, -> false
- else return FALSE
*/
}
As you can see the expected result (false
) and the result for a nonexistent element is the same (FALSE
) so you cannot check whether an element exists using the returned element value, an element key is different.
正如您可以看到预期的结果(false)和不存在的元素的结果是相同的(false),因此不能使用返回的元素值检查元素是否存在,因此元素键是不同的。
The key can either be an integer or a string. The value can be of any
type
. source键可以是整数,也可以是字符串。值可以是任何类型。源
The key()
returns the value of the current key if the element exists otherwise it will return NULL. A valid key can never be NULL so if null is returned we can determine that the element does not exist.
key()返回当前键的值,如果元素存在,则返回NULL。一个有效的键永远不会为空,所以如果返回NULL,我们可以确定元素不存在。