0

How can i delete an element from an array without using built-in functions in php ? But php doesn't have any keyword like delete or remove in it. can anyone help me ?

4
  • 3
    No AFAIK you have to use unset() or array_splice(). What's the problem with using functions, exactly? Commented Sep 30, 2021 at 8:10
  • No built-in functions should be used ? Commented Sep 30, 2021 at 8:12
  • Why not, exactly? It doesn't really make sense. Just use the correct tool for the job. In this case, that's the ready-made function. Have you been given this as a homework or something? Commented Sep 30, 2021 at 8:13
  • 1
    Unset is the fastest way and this is a language construct and not a function ;) Commented Sep 30, 2021 at 9:38

1 Answer 1

1

Okay so from what I can see there is no logical way to do this and why do you want to do this? PHP was built with these functions for a reason and that's why we all use them.

I recommend you use the following

  • Use unset() Function to Delete an Element From an Array in PHP
  • Use array_splice() Function to Delete an Element From an Array in PHP
  • Use array_diff() Function to Delete an Element From an Array in PHP

unset()

<?php
//Declare the array
$flowers = array(
                "Rose",
                "Lili",
                "Jasmine",
                "Hibiscus",
                "Tulip",
                "Sun Flower",
                "Daffodil",
                "Daisy");

unset($flowers[1]);
echo "The array is:\n";
print_r($flowers);
?>

This function can delete one value at a time. The name of the array along with the element index ($flowers[1]) is passed as a parameter. This function does not change the index values. The index values remain the same as they were before.

array_splice()

<?php
//Declare the array
$flowers = array(
                "Rose",
                "Lili",
                "Jasmine",
                "Hibiscus",
                "Tulip",
                "Sun Flower",
                "Daffodil",
                "Daisy");

array_splice($flowers, 4, 3);
echo "The array is:\n";
print_r($flowers);
?>

The array $flowers is passed as a parameter to this function along with the starting index 4 and the number of elements we want to delete-3. In this way, we can delete multiple elements from an array.

array_diff()

<?php
//Declare the array
$flowers = array(
                "Rose",
                "Lili",
                "Jasmine",
                "Hibiscus",
                "Tulip",
                "Sun Flower",
                "Daffodil",
                "Daisy");

$flowers = array_diff($flowers, array("Rose","Lili"));
echo "The array is:\n";
print_r($flowers);
?>

Here, the first array that we have passed is $flowers and the second array contains the elements that we want to delete from $flowers. This function does not change the indexes of the elements of the array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.