How to Remove Elements From an Array in PHP

Save

In PHP, a scripting language with a rich set of features, an array is an ordered map or a data type that contains associated keys and values. PHP has many functions that allow programmers to create, manipulate and otherwise operate on data stored in arrays, as well as functions that provide the ability to print array values in human-readable formats. PHP supports simple and multi-dimensional arrays and has functions that return arrays based on database values.

  • Open a text editor and create a new file named removeArray.php. Type an open "<?PHP" PHP tag delimiter and a close "?>" PHP tag delimiter to the file. The PHP program will interpret as code any text placed between these two delimiters when removeArray.php is opened from the Web server.

    <?PHP

    ?>

  • Declare an array named "$myFruits" that will hold three elements. The first element has the index "0" and the value "apple". The second element has the index "1" and the value "banana." The third element has the index "2" and the value "strawberry."

    <?PHP

    $myFruits = array(0 => "apple", 1 => "banana", 2 => "strawberry");

    ?>

  • Use a PHP "for loop" to loop through the elements in the $myFruits array. Use the variable "$i" initialized to "0" and use the $myFruits array indexes to retrieve each value. Use two curly braces as a placeholder for the loop's code.

    <?PHP

    $myFruits = array(0 => "apple", 1 => "banana", 2 => "strawberry");

    for($i = 0; $i<3; $i++)

    {

    }

    ?>

  • Use the PHP echo() function to write a line break ("") to the screen before each array value. Use the echo() function to write each of the $myFruits array values to the screen.

    <?PHP

    $myFruits = array(0 => "apple", 1 => "banana", 2 => "strawberry");

    for($i = 0; $i<3; $i++)

    {

    echo "";

    echo $myFruits[$i];

    }

    ?>

  • Type an unset() function immediately after the "for loop" closing brace ("}"). This function will remove and destroy the given item from the $myFruits array. Remove the item at array index 0. This is the item with the value "apple."

    <?PHP

    $myFruits = array(0 => "apple", 1 => "banana", 2 => "strawberry");

    for($i = 0; $i<3; $i++)

    {

    echo "";

    echo $myFruits[$i];

    }

    unset($myFruits[0]);

    ?>

  • Use a second PHP for loop construct to loop through the $myFruits array. As before, use the variable "$i" initialized to "0" and use the $myFruits array indices to retrieve each value. Use two curly braces as a placeholder for the loop's code.

    <?PHP

    $myFruits = array(0 => "apple", 1 => "banana", 2 => "strawberry");

    for($i = 0; $i<3; $i++)

    {

    echo "";

    echo $myFruits[$i];

    }

    unset($myFruits[0]);

    for($i = 0; $i<3; $i++)

    {

    }

    ?>

  • Use the PHP echo() function to write a line break ("") to the screen before each array value. Use the echo() function to write each of the $myFruits array values to the screen. Save and close removeArray.php.

    <?PHP

    $myFruits = array(0 => "apple", 1 => "banana", 2 => "strawberry");

    for($i = 0; $i<3; $i++)

    {

    echo "";

    echo $myFruits[$i];

    }

    unset($myFruits[0]);

    for($i = 0; $i<3; $i++)

    {

    echo "";

    echo $myFruits[$i];

    }

    ?>

  • Use a Web browser to open removeArray.php from the Web server. Verify that the $myFruits array originally contains the three values "apple," "banana" and "strawberry." After using the unset function to remove the first item from the array, the $myFruits array contains only "banana" and "strawberry."

Tips & Warnings

  • The unset() PHP function can be used to destroy other types of variables, not only those contained in arrays.
  • The PHP reset() function can be used to reset the internal array pointer to the first element in the array.
  • The unset() PHP function can be used to remove multiple items from an array. Use commas between the elements to remove multiple array items.
  • The array_splice() PHP function can be used to remove items from arrays.
  • The unset() function destroys the given variable so it is no longer valid. To maintain a variable that is targeted to be unset, store the value in another variable prior to unsetting it.

References

  • Photo Credit John Foxx/Stockbyte/Getty Images
Promoted By Zergnet

Comments

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!