Remove or delete elements from array in PHP
Orignal Source:http://codingvilla.com/remove-elements-array-php-single-article-575.aspx
Arrays:
Arrays are used to store data just like a variable; a variable store single data but arrays store multiple data. There are three types of array in php.
- Numeric Arrays
- Associative Arrays
- Multi-dimensional Arrays
Syntax:
$cars=array("Saab", “Volvo","BMW","Toyota"); //Numeric arrays
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); //Associative array
Remove array element in PHP:
To remove array element in php we use built in unset () function that was used for removing a variable in php. The unset () function takes element as a parameter and remove that element from array.
Code:
<?
$dogs = array('A' => 'C', 'B' => 'D','X' => 'Y', 'Q' => 'T');
printf("%s,", var_export($dogs, TRUE));
unset($dogs['X']);
printf("%s,", var_export($dogs, TRUE));
?>
Above is a simple code to remove an array element using unset () function in php.
This simple article tells that how we can remove an element from arrays in php.
Tags: remove, delete, elements, from, array, php
Comments
