在PHP中获取数组的前3个值 [英] Getting first 3 values of an array in PHP

查看:515
本文介绍了在PHP中获取数组的前3个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个看起来像这样的数组

Suppose I have an array that looks like this

array ([apple] => 1, [dog]=>2, [cat]=>5, [bread]=>9, [shoes]=> 4)

我可以打印数组的前三个值吗?如果是这样,怎么办?有任何想法吗?

Is it possible for me to print the first 3 values of the array? If so, how? Any ideas?

推荐答案

$firstThreeElements = array_slice($array, 0, 3);

其中0是您的偏移量,3是您想要的元素数。

Where 0 is your offset and 3 is the number of elements you want.

这篇关于在PHP中获取数组的前3个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆