PHP remove last element from an array

Today, We want to share with you PHP remove last element from an array.In this post we will show you Program to delete an element from array using unset() function, hear for remove last element from associative array php we will give you demo and example for implement.In this post, we will learn about Program to remove empty array elements in PHP with an example.

PHP remove last element from an array

There are the Following The simple About remove last element from an array Full Information With Example and source code.

Read Also:  jQuery Get the Text Value of Selected Option

As I will cover this Post with live Working example to develop php get last element of array and delete it., so the How to delete an element from an array in php? for this example is following below.

Example 1: Use the PHP array_pop() function

<?php
$products_category = array("Clothing", "Music", "Posters", "Albums");
 
// Deleting last array item using PHP
$removed_products_category = array_pop($products_category);
print_r($products_category);
echo "<br>";
var_dump($removed_products_category);
?>;

Example 2: Deleting an element from an array in PHP

 $array = array('0' => "laptop", 1 => "mobile", 2 => "table"];
    \unset($array[1]);

?>

Example 3: Associative arrays

$array = array('a' => 1, 'b' => 2, 'c' => 3);
unset($array['b']);

// RESULT: array('a' => 1, 'c' => 3)

Example 4: Follow the default functions:

$product_category_list = array("Laravel", "PHP", "MySQL", "JavaScript");

unset($product_category_list[2]);

$product_category_list = array("Laravel", "PHP", "MySQL", "JavaScript");

array_pop($product_category_list);

$product_category_list = array("Laravel", "PHP", "MySQL", "JavaScript");

array_splice($product_category_list,1,2);

$product_category_list = array("Laravel", "PHP", "MySQL", "JavaScript");

array_shift($product_category_list);

Example 5: How to Remove Elements from Arrays in PHP?

  // Our PHP initial array
  $arr = array("php", "angularjs", "vuejs", "magento", "angularjs", "wordpress", "magento", "reactjs", "vuejs");
  print_r($arr);

  // Remove/delete the elements who's values are magento or vuejs
  $arr = array_diff($arr, array("magento", "vuejs"));
  print_r($arr);

Web Programming Tutorials Example with Demo

Read :

Read Also:  Retrieve Multiple Checkbox Selected Options using VueJS

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP delete last element from an array.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Simple Print PHP Hello World program From scratch
  2. Read Also:  PHP Array Remove specific value Example
  3. Read Also:  PHP - Array Functions Example With Demo
  4. Read Also:  How to print or echo all the values of an array in PHP?
  5. Read Also:  Error incorrect format parameter phpmyadmin
  6. Read Also:  Angular 6 SEO (Search Engine Optimization) Tutorial
  7. Read Also:  how to add two numbers in javascript?
  8. Read Also:  PHP Multidimensional Array Check if specific array key
  9. Read Also:  JavaScript Remove Object from Array
  10. Read Also:  PHP cURL Http Authorization Pass Header Examples