Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Last 4 Elements From Array Php [duplicate]

Tags:

arrays

php

This is my array

$array=Array( 
                [0] => hell 
                [1] => for 
                [2] => sale 
                [3] => for 
                [4] => sale 
                [5] => earth 
                [6] => 0 
            )

and i want know how to remove last 4 elements from $array

and just i want get "hell for sale" from $array.any one know about this?

like image 440
Sasindu Jayampathi Avatar asked Dec 24 '22 12:12

Sasindu Jayampathi


1 Answers

You can try array_splice.

Try this code snippet here

<?php
$array = Array(
    0 => "hell",
    1 => "for", 
    2 => "sale", 
    3 => "for", 
    4 => "sale", 
    5 => "earth", 
    6 => 0);
array_splice($array, count($array) - 4, 4);
print_r($array);
like image 134
Sahil Gulati Avatar answered Feb 15 '23 03:02

Sahil Gulati