Using array_splice()

Using array_splice()

Original arrayRemovedAddedNew array

1. Adding two new elements to the middle

Array
(
    [0] => Steinbeck
    [1] => Kafka
    [2] => Tolkien
)
Array
(
)
Array
(
    [0] => Melville
    [1] => Hardy
)
Array
(
    [0] => Steinbeck
    [1] => Kafka
    [2] => Melville
    [3] => Hardy
    [4] => Tolkien
)

2. Replacing two elements with a new element

Array
(
    [0] => Steinbeck
    [1] => Kafka
    [2] => Tolkien
)
Array
(
    [0] => Steinbeck
    [1] => Kafka
)
Array
(
    [0] => Bronte
)
Array
(
    [0] => Bronte
    [1] => Tolkien
)

3. Removing the last two elements

Array
(
    [0] => Steinbeck
    [1] => Kafka
    [2] => Tolkien
)
Array
(
    [0] => Kafka
    [1] => Tolkien
)
Nothing
Array
(
    [0] => Steinbeck
)

4. Inserting a string instead of an array

Array
(
    [0] => Steinbeck
    [1] => Kafka
    [2] => Tolkien
)
Array
(
)
Orwell
Array
(
    [0] => Steinbeck
    [1] => Orwell
    [2] => Kafka
    [3] => Tolkien
)
 

CODE FOLLOWS

<?php
// Include the header file (assumed to contain common HTML and styles)
include('includes/header.php');
?>


    <!DOCTYPE html>
<html lang="en">
  <head>
    <title>Using array_splice()</title>
    <link rel="stylesheet" type="text/css" href="css/common.css" />
    <style type="text/css">
      h2, pre { margin: 1px; }
      table { margin: 0; border-collapse: collapse; width: 100%; }
      th { text-align: left; }
      th, td { text-align: left; padding: 4px; vertical-align: top; border:
1px solid gray; }
    </style>
  </head>
  <body>
    <h1>Using array_splice()</h1>

<?php

$headingStart 
'<tr><th colspan="4"><h2>';
$headingEnd '</h2></th></tr>';
$rowStart '<tr><td><pre>';
$nextCell '</pre></td><td><pre>';
$rowEnd '</pre></td></tr>';

echo 
'<table cellpadding="0" cellspacing="0"><tr><th>Original
array</th><th>Removed</th><th>Added</th><th>New array</th></tr>'
;

echo 
"{$headingStart}1. Adding two new elements to the middle{$headingEnd}";

$authors = array( "Steinbeck""Kafka""Tolkien" );
$arrayToAdd = array( "Melville""Hardy" );
echo 
$rowStart;

print_r$authors );
echo 
$nextCell;
print_rarray_splice$authors20$arrayToAdd ) );
echo 
$nextCell;
print_r$arrayToAdd );
echo 
$nextCell;
print_r$authors );
echo 
$rowEnd;
echo 
"{$headingStart}2. Replacing two elements with a new
element
{$headingEnd}";

$authors = array( "Steinbeck""Kafka""Tolkien" );
$arrayToAdd = array( "Bronte" );
echo 
$rowStart;
print_r$authors );
echo 
$nextCell;
print_rarray_splice$authors02$arrayToAdd ) );
echo 
$nextCell;
print_r$arrayToAdd );
echo 
$nextCell;
print_r$authors );
echo 
$rowEnd;

echo 
"{$headingStart}3. Removing the last two elements{$headingEnd}";

$authors = array( "Steinbeck""Kafka""Tolkien" );
echo 
$rowStart;
print_r$authors );
echo 
$nextCell;
print_rarray_splice$authors) );
echo 
$nextCell;
echo 
"Nothing";
echo 
$nextCell;
print_r$authors );
echo 
$rowEnd;

echo 
"{$headingStart}4. Inserting a string instead of an array{$headingEnd}";

$authors = array( "Steinbeck""Kafka""Tolkien" );
echo 
$rowStart;
print_r$authors );
echo 
$nextCell;
print_rarray_splice$authors10"Orwell" ) );
echo 
$nextCell;
echo 
"Orwell";
echo 
$nextCell;
print_r$authors );
echo 
$rowEnd;

echo 
'</table>';

?>
    </div>    <!-- end of content -->

    <div id="footer">
        <p>
            &bull; Copyright&copy; <?php echo(date("Y")); ?> Don Alexander Eckford (Dxander, DxanderDev)
        </p>
    </div>

    </div>    <!-- end of page -->
  </body>
</html>

<?php
// Include the footer file (assumed to contain closing HTML and scripts)
include('includes/footer.php');
?>