Text Size
Sunday, May 20, 2012
Get a look down
Friday, 27 January 2012 10:03

How to get the first N elements of an array in php

Written by Nabil Sadki
Rate this item
(0 votes)
Here's how you can get the first N elements of an array, it's so simple with the use of array_slice :
1
2
3
4
5
6
7
8
9
10
<?php
// Array
$myArray = array("bird", "dog", "cat", "horse", "lizard");
 
// Get the 2 first elements
$output = array_slice($myArray, 0, 2);
 
// Result
// This will give an array with elements : "bird", "dog"
?>
Nabil Sadki

Nabil Sadki

Hello, I am Nabyl, a Freelance web developer and a joomla addict. I have made many sites using the latter, but also patches for the core of this CMS. I like HipHop culture and world of warcraft. You can find find me on Twitter when i'm free...

Website: www.coins.ma E-mail: This e-mail address is being protected from spambots. You need JavaScript enabled to view it

Add comment



Usefull tricks