Thursday, February 25, 2010

Array Functions : How to insert an element or remove an element from an array

In this process you can insert an element or remove an element from an array


$numbers = array(1,2,3,4,5,6);
print_r($numbers);
echo "

";

// shifts first element out of an array
// and returns it.
$a = array_shift($numbers);
echo "a:" . $a ."
";
print_r($numbers);
echo "

";

// prepends an element to an array,
// returns the element count.
$b = array_unshift($numbers, 'first');
echo "b: ". $b ."
";
print_r($numbers);
echo "

";

echo "
";

// pops last element out of an array
// and returns it.
$a = array_pop($numbers);
echo "a: " . $a ."
";
print_r($numbers);
echo "

";

// pushes an element onto the end of an array,
// returns the element count.
$b = array_push($numbers, 'last');
echo "b: ". $b ."
";
print_r($numbers);
echo "

";

No comments:

Post a Comment

I just got my #domain @BigRock. Get upto 25% off with my personalized coupon link Get upto 25% off