Showing posts with label Automatically Added Element. Show all posts
Showing posts with label Automatically Added Element. Show all posts

An Issue in PHP Multidimensional Array

When assing an element of multidimensional array in PHP, such as:

$arr = array(array());
$idx = 0;
$arr['Key_1'][$idx] = $something;

indeed, there would be automatically generate another element in front:

$arr[0] = '';

As a result, there are two elements:

$arr[0] = '';
$arr['Key_1'][0] = 'something';

However, when keys do include [0], it would be no such issue. To fix up the problem, following code needs to be added before return the array:

unset($arr[0]);

Labels