array_splice function - remove some elements of the array and replace

array_splice function - remove some elements of the array and replace

array-splice-example-in-php.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title>array_splice function - remove some elements of the array and replace</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="Author" content="Md Iqbal Hosan">
  <meta name="title" content="array_splice function - remove some elements of the array and replace">
  <meta name="keywords" content="php, example, code, array, function, array_splice">
  <meta name="description" content="array_splice function - remove some elements of the array and replace">
 </head>
 <body>
  <h3>array_splice function - remove some elements of the array and replace</h3>
  <?php
   /*
    ------------------------------------------------
    let us make an array with some values
    ------------------------------------------------
   */
   $myArray = array("value1","value2","value3");
   echo "<p style='background:#ddd; color:red;'>Output of myArray</p>";
   echo "<pre>";
   print_r($myArray);
   echo "</pre>";
   /*
    -------------------------------------------------------------------------------------
    we want to slice up first two elements by using array_splice function
    -------------------------------------------------------------------------------------
   */
   $finalArray = array_splice($myArray, 2);
   echo "<p style='background:#ccc; color:red;'>Output of finalArray using array_splice function</p>";
   echo "<pre>";
   print_r($finalArray);
   echo "</pre>";
   /*
    --------------------------------------------------------------------------------------------------------
    myArray becomes
    --------------------------------------------------------------------------------------------------------
   */
   echo "<p style='background:#bbb; color:red;'>myArray becomes</p>";
   echo "<pre>";
   print_r($myArray);
   echo "</pre>";
   /*
    --------------------------------------------------------------------------------------------------------
    if we want to replace the 2nd element with something else we can do this by using array_splice function
    --------------------------------------------------------------------------------------------------------
   */
   array_splice($myArray, 1, 1, "modon");
   echo "<p style='background:#aaa; color:red;'>Output of myArray now using array_splice function : replaced by something</p>";
   echo "<pre>";
   print_r($myArray);
   echo "</pre>";
  ?>
 </body>
</html>

array_splice function - remove some elements of the array and replace - output in the browser

No comments:

Post a Comment

leave your comments here..