Broad Network


String and Integer Keyed Array with Security Considerations in PHP

PHP Arrays with Security Considerations

Foreword: In this part of the series, I talk about String and Integer Keyed Array with Security Considerations in PHP.

By: Chrysanthus Date Published: 7 Nov 2018

Introduction

This is part 4 of my series, PHP Arrays with Security Considerations. In this part of the series, I talk about String and Integer Keyed Array with Security Considerations in PHP. You should have read the previous parts of the series before coming here, as this is the continuation.

Array
In fact PHP has only one array. I have separated the array into indexed array, associative array and array with integer and string keys, to better explain the array. The keys of the indexed array are integers.

Imagine that you went to a party, and you met 7 new people. You can remember the names of 5 of them. For the remaining 2, you can remember or estimate their ages. You can remember the professions of the 7 people.

You can put that information into an array as follows:

    $arr = array('John' => "accountant", 'Paul' => "teacher", 'Peter' => "doctor", 'Mary' => "lawyer", 'Susan' => "nurse", 35 => "businessman", 42 => "contractor");

A PHP array actually consists of key/value pairs. The keys are separated from the values by the arrow operator, => . Each element (key/value) is separated from the other by a comma.

In many cases, the key is a string and the value is a string. The key can also be an integer. If a key is 8, it is an integer. If the 8 is written in quotes, like '8', it is still an integer. However, '08' where 8 is preceded by zero, in quotes, is a string and not an integer.

Try the following code:

<?php

    $arr = array('John' => "accountant", 'Paul' => "teacher", 'Peter' => "doctor", 'Mary' => "lawyer", 'Susan' => "nurse", 35 => "businessman", 42 => "contractor");

    echo $arr['Paul'], '<br>';
    echo $arr[35], '<br>';

?>

The output is:

    teacher
    businessman

Is_type Functions

The is_array() Function
This function returns true if its argument is an array and false if it is not.

<?php

    $var1 = array();
    $var2 = 55;

    if (is_array($var1))
        echo 'it is array', '<br>';
    else
        echo 'it is not array', '<br>';
    if (is_array($var2))
        echo 'it is array', '<br>';
    else
        echo 'it is not array', '<br>';

?>

The output is:

    it is array
    it is not array

New Index
When you delete elements from an array, there is no re-indexing. When you add a new index to the array, just by adding a value (no key specified), the new index is the largest index before, plus 1. Assume that you had an array with the largest index, 12 and you delete the element, implying that there is no more index of 12. If you now add a value (without index) to the array, the new index would be 13. This is not usually a problem.

Occassionally you will need re-indexing after deleting elements. Try the following code:

<?php

    $arr = array(0=>'A', 'aa' => 'woman', 1=>'B', 2=>'C', 'bb' => 'man', 3=>'D', 4=>'E');

    unset($arr[2]);
    unset($arr[4]);

    foreach ($arr as $key => $value)
        echo $key, ' => ', $value, '<br>';

    array_push($arr, 'F');

    echo '<br>';

    foreach ($arr as $key => $value)
        echo $key, ' => ', $value, '<br>';

    $arr = array_values($arr);

    echo '<br>';

    foreach ($arr as $key => $value)
        echo $key, ' => ', $value, '<br>';

    array_push($arr, 'G');

    echo '<br>';

    foreach ($arr as $key => $value)
        echo $key, ' => ', $value, '<br>';

?>

The unset() function deletes an element. Reading the value of the deleted element, using the key, returns NULL.

The output is:

0 => A
aa => woman
1 => B
bb => man
3 => D

0 => A
aa => woman
1 => B
bb => man
3 => D
5 => F

0 => A
1 => woman
2 => B
3 => man
4 => D
5 => F

0 => A
1 => woman
2 => B
3 => man
4 => D
5 => F
6 => G

Read the code (program) to appreciate the output.

Note that with the use of the array_values() function, all string keys were replaced by integers. If you really want to do re-indexing and preserve the keys, use the array_slice() function as in the following code:

<?php

    $arr = array(0=>'A', 'aa' => 'woman', 1=>'B', 2=>'C', 'bb' => 'man', 3=>'D', 4=>'E');

    unset($arr[2]);
    unset($arr[4]);

    foreach ($arr as $key => $value)
        echo $key, ' => ', $value, '<br>';

    array_push($arr, 'F');

    echo '<br>';

    foreach ($arr as $key => $value)
        echo $key, ' => ', $value, '<br>';

    $arr = array_slice($arr, 0);

    echo '<br>';

    foreach ($arr as $key => $value)
        echo $key, ' => ', $value, '<br>';

    array_push($arr, 'G');

    echo '<br>';

    foreach ($arr as $key => $value)
        echo $key, ' => ', $value, '<br>';

?>

The output now is:

0 => A
aa => woman
1 => B
bb => man
3 => D

0 => A
aa => woman
1 => B
bb => man
3 => D
5 => F

0 => A
aa => woman
1 => B
bb => man
2 => D
3 => F

0 => A
aa => woman
1 => B
bb => man
2 => D
3 => F
4 => G

You slice from the beginning to the end of the array.

That is it for this part of the series. We stop 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