PHP - 如何从数组中获取最后一个元素之前的元素?

发布于 2024-08-20 12:24:24 字数 33 浏览 1 评论 0原文

如何从 PHP5 数组中获取最后一个元素之前的元素?

How can I get the element before the last element from an array in PHP5 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

丢了幸福的猪 2024-08-27 12:24:24

即使在此数组上,这也适用:

$array[0] = "hello";
$array[5] = "how";
$array[9] = "are";

end($array);
echo prev($array); // will print "how"

使用 count() 的其他解决方案假设数组的索引按顺序排列;通过使用 end 和 prev 移动数组指针,您可以获得实际值。尝试对上面的数组使用 count() 方法,它会失败。

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.

丢了幸福的猪 2024-08-27 12:24:24
$array[count($array)-2]

它应该是一个数字索引数组(从零开始)。您应该至少有 2 个元素才能使其发挥作用。 (明显地)

$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)

长发绾君心 2024-08-27 12:24:24

array_slice 采用负偏移量作为第二个参数。这将为您提供一个包含倒数第二个项目的单个项目数组:

$arr = array(1,2,3,4,5,6);
array_slice($arr, -2, 1);

如果您只想要单个值,您有多种选择。如果您不介意使用中间变量,则可以使用 [0] 获取第一个值或调用 array_pop 或 array_shift,它们都需要通过引用传递的变量,否则您将在严格模式下收到警告。

或者您可以只使用 array_sum 或 array_product,这有点 hacky,但对于单个项目数组来说效果很好。

array_slice takes a negative offset as the second argument. This will give you a single item array containing the second last item:

$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.

Or you could just use array_sum or array_product, which is a bit hacky but works fine for single item arrays.

吻安 2024-08-27 12:24:24

适用于关联数组和数值数组的方法是使用array_pop()将元素从数组末尾弹出。

$last = array_pop($array);
$second_last = array_pop($array);

// put back the last
array_push($array, $last);

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.

$last = array_pop($array);
$second_last = array_pop($array);

// put back the last
array_push($array, $last);
沉溺在你眼里的海 2024-08-27 12:24:24

所有数组都有一个指向当前数组元素的“内部数组指针”,PHP 有几个函数允许您浏览数组并查看当前元素的键和值。

无论数组是空的、顺序的还是关联的,这些函数都可以工作,并且由于在示例中尚未指定数组,我假设这必须适用于任何阵列。

$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 
    */
}

正如您所看到的,预期结果 (false) 和不存在元素的结果是相同的 (FALSE),因此您无法使用返回的元素值检查元素是否存在,元素键不同。

密钥可以是 整数字符串。该值可以是任何类型来源

key() 如果元素存在则返回当前键的值,否则返回 NULL 。
有效的键永远不能为 NULL,因此如果返回 null,我们可以确定该元素不存在。

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.

  • end() - Set the internal pointer of an array to its last element
  • reset() - Set the internal pointer of an array to its first element
  • prev() - Rewind the internal array pointer
  • next() - Advance the internal array pointer of an array
  • current() - Return the current element in an array
  • key() - Fetch a key from an array
  • each() - Return the current key and value pair from an array and advance the array cursor

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.

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.

谎言月老 2024-08-27 12:24:24
// 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];
// 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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文