Delete element from array using PHP

Here is some easy to read code that removes an element from an array.

$array = array(“red”, “blue”, “green”, “white”, “purple”);
$arraycount = count($array);//array
$removeelement = “green”;//element to remove from array
$newarray =array();//new array with element removed
for ($i = 0; $i < $arraycount; $i++) {
if(!strchr($array[$i], $removeelement )){
array_push($newarray, $array[$i]);
}
}
print_r($newarray);

Advertisements

Published by

angelo bonavera

Hi I'm a web developer and I run a web hosting company called 2slick.com.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s