简体   繁体   English

我如何返回键大于X的数组值

[英]How can i return array values where key is greater than X

I am storing user messages in an array and i want to filter out the nonsense and store the valuable data in a log. 我将用户消息存储在数组中,我想过滤掉废话并将有价值的数据存储在日志中。

The array size is being generated by the user message which means the array can be any size depending on the size of the messsage. 用户消息正在生成数组大小,这意味着数组可以是任何大小,具体取决于消息的大小。

So lets give an example 所以举个例子

Say i have an array that looks like the following: 说我有一个看起来像下面的数组:

Array
(
[0] => some nonsense
[1] => some more nonsense
[2] => even more nonsense
[3] => valuable data
[4] => some more valuable data
[5] => even more valuable data
[6] => so much valuable data
)

We basically want to ignore array keys 0, 1 and 2 which leaves me with the data that i want so i can store it in a log file 我们基本上想忽略数组键0、1和2,这给了我我想要的数据,所以我可以将其存储在日志文件中

Regards. 问候。

You can do this using array_slice : 您可以使用array_slice做到这一点:

array array_slice (
    array $array , int $offset [, int $length = NULL [, bool $preserve_keys = false ]]
)

In your case, you should do: 对于您的情况,您应该执行以下操作:

$arr = array_slice($arr, 3);  // Gives you from [3], [4]... till the end.

For the offset parameter to be 3 : 对于offset参数为3

If offset is non-negative, the sequence will start at that offset in the array. 如果offset为非负数,则序列将从数组中的该偏移量开始。 If offset is negative, the sequence will start that far from the end of the array. 如果offset为负,则序列将从数组末尾开始。

If you want to preserve the keys, you need to give another true at the end. 如果要保留密钥,则需要在末尾给出另一个true

$arr = array_slice($arr, 3, null, true);

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

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