How to Array Remove Numeric Keys in PHP?

03-Apr-2023

.

Admin

How to Array Remove Numeric Keys in PHP?

Hi Dev,

This tutorial will give you simple example how to array remove first an elements in php. you will learn remove first n element in array php with code examples. Follow bellow tutorial step remove first n specific element from array. this example will help you use the only keep the first n elements of an array in php. You just need to some step to done array_shift function user remove first n elements in php.

The array_shift() function removes the first element from an array, and returns the value of the removed element. This function accepts single parameter variable. It is required parameter and used to unset the element.

Example 1:


index.php

<?php

$a=array("a"=>"red", "b"=>"green", "c"=>"blue");

echo array_shift($a)."<br>";

print_r ($a);

?>

Output:

red

Array ( [b] => green [c] => blue )

Example 2:

index.php

<?php

// PHP program to delete an array

// element based on index

// Declare arr variable

// and initialize it

$arr = array('N', 'I', 'C', 'E', 'S');

echo "<pre>";

// Display the array element

print_r($arr);

// Use unset() function delete

// elements

unset($arr[4]);

// Display the array element

print_r($arr);

?>

Output:

Array

(

[0] => N

[1] => I

[2] => C

[3] => E

[4] => S

)

Array

(

[0] => N

[1] => I

[2] => C

[3] => E

)

I hope it could help you...

#PHP