获取php数组的最后5个元素

时间:2022-10-24 17:23:26

How can I get last 5 elements of a PHP array.

如何获取PHP数组的最后5个元素。

Array is dynamically generated by MySQL query result. length is not fixed. If length <= 5 then get all else last 5.

数组是由MySQL查询结果动态生成的。长度不固定。如果长度<= 5,那么将其他所有其他5个。

I tried with PHP function like last() and array_pop() but they gives only last element.

我尝试使用像last()和array_pop()这样的PHP函数,但它们只给出了最后一个元素。

Please help me to solve it.

请帮我解决一下。

5 个解决方案

#1


22  

You need array_slice, which does exactly this.

你需要array_slice,它正是这样做的。

$items = array_slice($items, -5);

-5 means "start at five elements before the end of the array".

-5表示“在数组结束前从五个元素开始”。

#2


5  

array_pop() 5 times in a loop? If the returned value is a null, you've exhausted the array.

array_pop()循环5次?如果返回的值为null,则表示您已经耗尽了数组。

$lastFive = array();

for($i=0;$i < 5;$i++)
{
    $obj = array_pop($yourArray);

    if ($obj == null) break;
    $lastFive[] = $obj;
}

After seeing the other answers, I have to admit array_slice() looks shorter and more readable.

看到其他答案后,我不得不承认array_slice()看起来更短,更具可读性。

#3


3  

array_slice($array, -5) should do the trick

array_slice($ array,-5)应该可以解决问题

#4


0  

It's simple use array_slice and count()

使用array_slice和count()很简单

$arraylength=count($array);

    if($arraylength >5)
         $output_array= array_slice($array,($arraylength-5),$arraylength);
    else
        $output_array=$array;

#5


0  

I just want to extend the question a little bit. What if you looping though a big file and want to keep the last 5 lines or 5 elements from a current position. And you don't want to keep huge array in a memory and having problems with the performance of array_slice.

我只是想稍微扩展一下这个问题。如果您循环通过一个大文件并希望保留当前位置的最后5行或5个元素,该怎么办?并且您不希望将大型数组保留在内存中并且遇到array_slice的性能问题。

This is a class that implements ArrayAccess interface.

这是一个实现ArrayAccess接口的类。

It gets array and desired buffer limit.

它获得数组和所需的缓冲区限制。

You can work with the class object like it's an array but it will automatically keep ONLY last 5 elements

您可以使用类对象,就像它是一个数组,但它将自动保留最后5个元素

<?php
class MyBuffer implements ArrayAccess {
    private $container;
    private $limit;

    function __construct($myArray = array(), $limit = 5){
        $this->container = $myArray;
        $this->limit = $limit;
    }
    public function offsetSet($offset, $value) {
        if (is_null($offset)) {
            $this->container[] = $value;
        } else {
            $this->container[$offset] = $value;
        }
        $this->adjust();
    }

    public function offsetExists($offset) {
        return isset($this->container[$offset]);
    }

    public function offsetUnset($offset) {
        unset($this->container[$offset]);
    }

    public function offsetGet($offset) {
        return isset($this->container[$offset]) ? $this->container[$offset] : null;
    }
    public function __get($offset){
        return isset($this->container[$offset]) ? $this->container[$offset] : null;
    }
    private function adjust(){
        if(count($this->container) == $this->limit+1){
            $this->container = array_slice($this->container, 1,$this->limit);
        }
    }
}


$buf = new MyBuffer();
$buf[]=1;
$buf[]=2;
$buf[]=3;
$buf[]=4;
$buf[]=5;
$buf[]=6;

echo print_r($buf, true);

$buf[]=7;
echo print_r($buf, true);


echo "\n";
echo $buf[4];

#1


22  

You need array_slice, which does exactly this.

你需要array_slice,它正是这样做的。

$items = array_slice($items, -5);

-5 means "start at five elements before the end of the array".

-5表示“在数组结束前从五个元素开始”。

#2


5  

array_pop() 5 times in a loop? If the returned value is a null, you've exhausted the array.

array_pop()循环5次?如果返回的值为null,则表示您已经耗尽了数组。

$lastFive = array();

for($i=0;$i < 5;$i++)
{
    $obj = array_pop($yourArray);

    if ($obj == null) break;
    $lastFive[] = $obj;
}

After seeing the other answers, I have to admit array_slice() looks shorter and more readable.

看到其他答案后,我不得不承认array_slice()看起来更短,更具可读性。

#3


3  

array_slice($array, -5) should do the trick

array_slice($ array,-5)应该可以解决问题

#4


0  

It's simple use array_slice and count()

使用array_slice和count()很简单

$arraylength=count($array);

    if($arraylength >5)
         $output_array= array_slice($array,($arraylength-5),$arraylength);
    else
        $output_array=$array;

#5


0  

I just want to extend the question a little bit. What if you looping though a big file and want to keep the last 5 lines or 5 elements from a current position. And you don't want to keep huge array in a memory and having problems with the performance of array_slice.

我只是想稍微扩展一下这个问题。如果您循环通过一个大文件并希望保留当前位置的最后5行或5个元素,该怎么办?并且您不希望将大型数组保留在内存中并且遇到array_slice的性能问题。

This is a class that implements ArrayAccess interface.

这是一个实现ArrayAccess接口的类。

It gets array and desired buffer limit.

它获得数组和所需的缓冲区限制。

You can work with the class object like it's an array but it will automatically keep ONLY last 5 elements

您可以使用类对象,就像它是一个数组,但它将自动保留最后5个元素

<?php
class MyBuffer implements ArrayAccess {
    private $container;
    private $limit;

    function __construct($myArray = array(), $limit = 5){
        $this->container = $myArray;
        $this->limit = $limit;
    }
    public function offsetSet($offset, $value) {
        if (is_null($offset)) {
            $this->container[] = $value;
        } else {
            $this->container[$offset] = $value;
        }
        $this->adjust();
    }

    public function offsetExists($offset) {
        return isset($this->container[$offset]);
    }

    public function offsetUnset($offset) {
        unset($this->container[$offset]);
    }

    public function offsetGet($offset) {
        return isset($this->container[$offset]) ? $this->container[$offset] : null;
    }
    public function __get($offset){
        return isset($this->container[$offset]) ? $this->container[$offset] : null;
    }
    private function adjust(){
        if(count($this->container) == $this->limit+1){
            $this->container = array_slice($this->container, 1,$this->limit);
        }
    }
}


$buf = new MyBuffer();
$buf[]=1;
$buf[]=2;
$buf[]=3;
$buf[]=4;
$buf[]=5;
$buf[]=6;

echo print_r($buf, true);

$buf[]=7;
echo print_r($buf, true);


echo "\n";
echo $buf[4];