Broad Network


One Dimensional Array Functions on PHP Two Dimensional Array

PHP Multi-Dimensional Array with Security Consideration - Part 2

PHP 2D Indexed Array

Foreword: In this part of the series, I explain how PHP one-dimensional array functions can be used on PHP 2D array.

By: Chrysanthus Date Published: 23 Jan 2019

Introduction

This is part 2 of my series, PHP Multi-Dimensional Array. In this part of the series, I explain how PHP one-dimensional array functions can be used on PHP 2D array. You should have read the previous part of the series before reaching here, as this is the continuation.

Length of a Two-Dimensional Array
The length of a one-dimensional array is determined using the count() function. When you do this for a two-dimensional array, you obtain the number of rows. Read and test the following program:

<?php
    
    $arr;

    $arr[0][0] = "ProductID";
    $arr[0][1] = "ProductName";
    $arr[0][2] = "Category";
    $arr[1][0] = 1;
    $arr[1][1] = "TV Set";
    $arr[1][2] = "Entertainment";

    $arrLength = count($arr);

    print $arrLength;

?>

The output is,

    2

for two rows.

This same method is used for array by initialization. The following program illustrates this:

<?php
    
    $twoDArr = [
                     ["ProductID", "ProductName", "Category"],
                     [1, "TV Set", "Entertainment"]
                 ];

    $arrLength = count($twoDArr);

    print $arrLength;

?>

The output is,
    2

for 2 rows.

Slicing a 2D Array by Rows
Slicing a 2D Array by Rows means obtaining a range of rows in the 2D array. You use the slice function for this. The basic syntax is:

    array array_slice ($array, $offset [, $length ] )

The return object is an array (the slice). The return new array can be assigned to a new array variable. Nothing is deleted in the original array. The return array is a 2D array and you would access it as you would access other two-dimensional arrays. Read and test the following script that illustrates this:

<?php

    $arr[0] = ["ProductID", "ProductName", "Category", "Number", "CostPrice", "SellingPrice"];
    $arr[1] = [1, "TV Set", "Entertainment", 50, 25, 30];
    $arr[2] = [2, "DVD", "Entertainment", 50, 20, 25];
    $arr[3] = [3, "Clothe Box", "Household", 45, 16, 21];
    $arr[4] = [4, "Perfume", "Beauty", 100, 2, 3];
    $arr[5] = [5, "Banana", "Fruit", 125, 5, 7];
    $arr[6] = [6, "Pear", "Fruit", 135, 3, 4];

    $slicedArr = array_slice ($arr, 2, 4);

    echo $slicedArr[0][3], "<br>";
    echo $slicedArr[1][3], "<br>";
    echo $slicedArr[2][3], "<br>";
    echo $slicedArr[3][3], "<br>";

?>

The output is:

    50
    45
    100
    125

Note from the above code that you do not have to declare an array first, before you start using it by assigning a value.

Other Array Functions
One-dimensional array functions can be used with two-dimensional arrays, row-by-row. I assume that you have been reading the tutorials in this course (see below) in the order given. If you have not been doing that, then you may have problems understanding this tutorial.

Pushing A Row
You can add a row at the end of a 2D array. You use the 1D push function. The row added is a 1D array.

The syntax of the push function for adding a 1D array row at the bottom of a 2D array, is:

    array_push(Array, Item);

In the following program, two rows are added to a 2D array, using the push function twice:

<?php

    $arr;

    $arr[0] = ["ProductID", "ProductName", "Category", "Number", "CostPrice", "SellingPrice"];
    $arr[1] = [1, "TV Set", "Entertainment", 50, 25, 30];
    $arr[2] = [2, "DVD", "Entertainment", 50, 20, 25];
    $arr[3] = [3, "Clothe Box", "Household", 45, 16, 21];
    $arr[4] = [4, "Perfume", "Beauty", 100, 2, 3];

    $rowF = [5, "Banana", "Fruit", 125, 5, 7];

    $rowG = [6, "Pear", "Fruit", 135, 3, 4];

    array_push($arr, $rowF);
    array_push($arr, $rowG);

    print $arr[5][1] . "<br>";
    print $arr[6][1] . "<br>";

?>

The output is:

    Banana
    Pear

The push function returns the new number of rows in the array; this number is the old number plus 1 for any push function call.

Unshifting a Row
Unshifting does the opposite of pushing. It prepends a row in front of a 2D array, returning the new number of rows in the resulting array. The syntax is:

    array_unshift (Array, Item);

In the following program, a row is unshifted to the beginning of a 2D array:

<?php

    $arr;

    $arr[0] = [1, "TV Set", "Entertainment", 50, 25, 30];
    $arr[1] = [2, "DVD", "Entertainment", 50, 20, 25];
    $arr[2] = [3, "Clothe Box", "Household", 45, 16, 21];
    $arr[3] = [4, "Perfume", "Beauty", 100, 2, 3];
    $arr[4] = [5, "Banana", "Fruit", 125, 5, 7];

    $rowA = ["ProductID", "ProductName", "Category", "Number", "CostPrice", "SellingPrice"];

    array_unshift($arr, $rowA);

    echo $arr[0][0] . "<br>";

?>

The output is:

    ProductID

Popping a Row
The pop function removes the last row from the array and returns it, shortening the original array by one row. The syntax is,

    array_pop (Array);

In the following program the last row is popped out:

<?php

    $arr;

    $arr[0] = ["ProductID", "ProductName", "Category", "Number", "CostPrice", "SellingPrice"];
    $arr[1] = [1, "TV Set", "Entertainment", 50, 25, 30];
    $arr[2] = [2, "DVD", "Entertainment", 50, 20, 25];
    $arr[3] = [3, "Clothe Box", "Household", 45, 16, 21];
    $arr[4] = [4, "Perfume", "Beauty", 100, 2, 3];
    $arr[5] = [5, "Banana", "Fruit", 125, 5, 7];
    $arr[6] = [6, "Pear", "Fruit", 135, 3, 4];

    $oldArrayLength = count($arr);
    echo $oldArrayLength, "<br>";

    $poppedArrRow= array_pop($arr);

    echo $poppedArrRow[1], "<br>";

    $newArrayLength = count($arr);
    echo $newArrayLength, "<br>";

?>

The output is,

    7
    Pear
    6

Do read and test all the code samples of this tutorial.

Shifting a Row
The shift function does the opposite of the pop function. It removes the first row from the array and returns it, shortening the original array by one row. The syntax is,

    array_shift (Array);

In the following program the first row is shifted out:

<?php

    $arr;

    $arr[0] = ["ProductID", "ProductName", "Category", "Number", "CostPrice", "SellingPrice"];
    $arr[1] = [1, "TV Set", "Entertainment", 50, 25, 30];
    $arr[2] = [2, "DVD", "Entertainment", 50, 20, 25];
    $arr[3] = [3, "Clothe Box", "Household", 45, 16, 21];
    $arr[4] = [4, "Perfume", "Beauty", 100, 2, 3];
    $arr[5] = [5, "Banana", "Fruit", 125, 5, 7];
    $arr[6] = [6, "Pear", "Fruit", 135, 3, 4];

    $oldArrayLength = count($arr);
    echo $oldArrayLength, "<br>";

    $shiftedArrRow = array_shift($arr);

    echo $shiftedArrRow[1], "<br>";

    $newArrayLength = count($arr);
    echo $newArrayLength, "<br>";

?>

Splicing a Range of Rows
The splice function removes a portion (range) of rows from the array and replaces it with that of some other 2D array; if that is present. The resulting array may be larger or smaller than the original array depending on how many rows are in the replacement 2D Array. If there is no replacement array, then the resulting array is shorter. The splice function returns the 2D array of the elements removed. The basic syntax is,

    array_splice(ARRAY, OFFSET, LENGTH, REPLACEMENTARRAY)

ARRAY is the name of the original array. OFFSET is the index at which the removal will start. Remember, index counting begins from zero. LENGTH is the number of consecutive rows to be removed. REPLACEMENTARRAY, if present has the rows to replace the ones removed.

For our example, imagine that you want to remove 3 rows beginning from index 2 (which is position 3) and replace them with the following array:

    $arrReplace;

    $arrReplace [0] = ["2,0", "2,1", "2,2", "2,3", "2,4", "2,5"];
    $arrReplace [1] = ["3,0", "3,1", "3,2", "3,3", "3,4", "3,5"];
    $arrReplace [2] = ["4,0", "4,1", "4,2", "2,3", "4,4", "4,5"];


The splice function would be,

    array_splice($arr, 2, 3, $arrReplace);

So, 3 rows will be removed beginning from index 2. The LENGTH is 3.

The following script illustrates this:

<?php

    $arr;

    $arr[0] = ["ProductID", "ProductName", "Category", "Number", "CostPrice", "SellingPrice"];
    $arr[1] = [1, "TV Set", "Entertainment", 50, 25, 30];
    $arr[2] = [2, "DVD", "Entertainment", 50, 20, 25];
    $arr[3] = [3, "Clothe Box", "Household", 45, 16, 21];
    $arr[4] = [4, "Perfume", "Beauty", 100, 2, 3];
    $arr[5] = [5, "Banana", "Fruit", 125, 5, 7];
    $arr[6] = [6, "Pear", "Fruit", 135, 3, 4];

    $arrReplace;

    $arrReplace[0] = ["2,0", "2,1", "2,2", "2,3", "2,4", "2,5"];
    $arrReplace[1] = ["3,0", "3,1", "3,2", "3,3", "3,4", "3,5"];
    $arrReplace[2] = ["4,0", "4,1", "4,2", "2,3", "4,4", "4,5"];

    $removedArr = array_splice($arr, 2, 3, $arrReplace);

    for ($i=0; $i<7; ++$i)
        {
            for ($j=0; $j<6; ++$j)
                {
                    echo $arr[$i][$j] . ", ";
                }
            print "<br>";        
        }

    print "<br>";

    for ( $i=0; $i<3; ++$i)
        {
            for ($j=0; $j<6; ++$j)
                {
                    echo $removedArr[$i][$j] . ", ";
                }
            print "<br>";        
        }

?>

The output is:

ProductID, ProductName, Category, Number, CostPrice, SellingPrice,
1, TV Set, Entertainment, 50, 25, 30,
2,0, 2,1, 2,2, 2,3, 2,4, 2,5,
3,0, 3,1, 3,2, 3,3, 3,4, 3,5,
4,0, 4,1, 4,2, 2,3, 4,4, 4,5,
5, Banana, Fruit, 125, 5, 7,
6, Pear, Fruit, 135, 3, 4,

2, DVD, Entertainment, 50, 20, 25,
3, Clothe Box, Household, 45, 16, 21,
4, Perfume, Beauty, 100, 2, 3,

Do not forget to read and try the code.

Security Considerations
The insecurities and prevention of a 2D array can be deduced from those of a 1D array. I covered that in the series, PHP Arrays with Security Considerations.

That is it for this part of the series. We take a beak here and continue in the next part.

Chrys


Related Links

Basics of PHP with Security Considerations
White Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links

Cousins

BACK NEXT

Comments