Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,938 questions

51,875 answers

573 users

How to extract a slice of array (copy part of array) in PHP

3 Answers

0 votes
$myarray = [1, 4, -98, 10918, 120];

$ends = array_slice($myarray, 2); 

print_r($ends);

 
/*
run: 

Array ( [0] => -98 [1] => 10918 [2] => 120 )

*/

 



answered Oct 28, 2017 by avibootz
0 votes
$myarray = [1, 4, -98, 10918, 120];

$ends = array_slice($myarray, -3, 3); 
    
print_r($ends);

 
/*
run: 

Array ( [0] => -98 [1] => 10918 [2] => 120 )

*/

 



answered Oct 28, 2017 by avibootz
0 votes
$myarray = [1, 4, -98, 10918, 120];

$slice = array_slice($myarray, 0, 3); 

print_r($slice);

 
/*
run: 

Array ( [0] => 1 [1] => 4 [2] => -98 )

*/

 



answered Oct 28, 2017 by avibootz
...