Get more PHP array knowledge under your belt.
Hi there! Remember the PHP array tip series I started some months ago? Sorry for the delay on the second installment, but things have been crazy. Now I'm back!
This time, I'll take you through some of the pitfalls that I discovered when starting to work with arrays. I'll also show you some ways to prevent your code from failing with run time errors that you might not have seen coming.
First, I'll give you some fundamental knowledge of how arrays are allocated in PHP and explain some of the things that are very different from RPG.
Let's get started!
More About Defining Arrays and the Pitfalls They Can Cause
Unlike with RPG, you don't have to specify the number of elements in an array in PHP. You simply add elements to the array when you need to, and the array will automatically be increased. (I know you can do the same in RPG with dynamic arrays, but that's not the common way as far as I know).
But unlike RPG, where you define an array that consists of 50 elements and the program knows that you have all the elements lined up from 1 to 50, this is not necessary in PHP, and one pitfall you might stumble into when starting to work with PHP arrays is that you can "jump" in the keys of the array, which means that you can leave out keys and get "holes" in the array.
In the example below in Listing 1, I have created an array that does just that.
<?php
// Define an array
$wrkAry = array(0=>"Elvis Costello",
1=>"Nick Cave",
2=>"Interpol",
4=>"The Police",
5=>"Sea Pink"
);
// Print out the array
print_r($wrkAry);
// Jump and add an entry
$wrkAry[12] = "Nada Surf";
$wrkAry[] = "Calexico";
echo "\n";
// Print out the array
print_r($wrkAry);
?>
Listing 1: Define an array with "jumped" keys.
This code will give the following output:
Array
(
[0] => Elvis Costello
[1] => Nick Cave
[2] => Interpol
[4] => The Police
[5] => Sea Pink
)
Array
(
[0] => Elvis Costello
[1] => Nick Cave
[2] => Interpol
[4] => The Police
[5] => Sea Pink
[12] => Nada Surf
[13] => Calexico
)
Note that after the jump, when I add one more entry to the array, PHP will automatic increase the counter and add the next entry—in this case, it's "Calexico," which will be added as entry 13.
So far so good, but what about elements 6 to 11? Well, in RPG you could access them and they would just be empty, but in PHP, this is different because the elements do not exist. Let's look at looping through the code in example 2 in Listing 2.
<?php
// Define an array
$wrkAry = array(0=>"Elvis Costello",
1=>"Nick Cave",
2=>"Interpol",
4=>"The Police",
5=>"Sea Pink"
);
// Jump and add an entry
$wrkAry[12] = "Nada Surf";
$wrkAry[] = "Calexico";
// Get array elements and show how many
$loop = count($wrkAry);
echo "Nbr of elements is: $loop <hr>";
// Loop through the using a for loop
for ($i=0; $i<=$loop; $i++) {
echo "The key is: $i / The value is: $wrkAry[$i] <br>";
}
?>
Listing 2: Loop through the array.
This would give you the following output:
Nbr of elements is: 7
The key is: 0 / The value is: Elvis Costello
The key is: 1 / The value is: Nick Cave
The key is: 2 / The value is: Interpol
Notice: Undefined offset: 3 in C:\htdocs\mcpressonline\PHP Arrays - part 2\code\ex2.php on line 23
The key is: 3 / The value is:
The key is: 4 / The value is: The Police
The key is: 5 / The value is: Sea Pink
Notice: Undefined offset: 6 in C:\htdocs\mcpressonline\PHP Arrays - part 2\code\ex2.php on line 23
The key is: 6 / The value is:
Notice: Undefined offset: 7 in C:\htdocs\mcpressonline\PHP Arrays - part 2\code\ex2.php on line 23
The key is: 7 / The value is:
Hmm. What happens here? Well, two things:
- 1.The number of elements in the array found by using the count() function is only 7, which of course is correct because only 7 active elements are specified in the array.
- 2.When looping through the array, elements 6 and 7 will yield an error because nothing is assigned to these elements (big difference from RPG!).
So if you want to make your code bulletproof, you must use the max() function, which will give you the highest key value of the array and also the if(isset(something) to check if an element is in use and avoid the runtime error.
This would make your code look like the one in example 3 in Listing 3:
<?php
// Define an array
$wrkAry = array(0=>"Elvis Costello",
1=>"Nick Cave",
2=>"Interpol",
4=>"The Police",
5=>"Sea Pink"
);
// Jump and add an entry
$wrkAry[12] = "Nada Surf";
$wrkAry[] = "Calexico";
// Get array elements and show how many
$loop = count($wrkAry);
echo "Nbr of elements is: $loop <hr>";
$max = max(array_keys($wrkAry));
echo "Highest key value is: $max <hr>";
// Move it to the loop variable
$loop = $max;
// Loop through the using a for loop
for ($i=0; $i<=$loop; $i++) {
if(isset($wrkAry[$i])) {
echo "The key is: $i / The value is: $wrkAry[$i] <br>";
} else {
echo "<b>Element $i is empty</b><br>";
}
}
?>
Listing 3: Loop through the array and make sure all elements are shown and empty elements don't shout "error."
This will give you the following output:
Nbr of elements is: 7
Highest key value is: 13
The key is: 0 / The value is: Elvis Costello
The key is: 1 / The value is: Nick Cave
The key is: 2 / The value is: Interpol
Element 3 is empty
The key is: 4 / The value is: The Police
The key is: 5 / The value is: Sea Pink
Element 6 is empty
Element 7 is empty
Element 8 is empty
Element 9 is empty
Element 10 is empty
Element 11 is empty
The key is: 12 / The value is: Nada Surf
The key is: 13 / The value is: Calexico
Of course, you could also have used the code in example 4 in Listing 4.
<?php
// Define an array
$wrkAry = array(0=>"Elvis Costello",
1=>"Nick Cave",
2=>"Interpol",
4=>"The Police",
5=>"Sea Pink"
);
// Jump and add an entry
$wrkAry[12] = "Nada Surf";
$wrkAry[] = "Calexico";
// Loop through the array using foreach
foreach ($wrkAry as $key => $value) {
echo 'Key: ' . $key . ' / Value: ' . $value .'<br>';
}
?>
Listing 4: Loop through the array using the foreach() function.
But the code in example 4 would not tell you which elements were empty, so depending on the task, you would have to use the loop method that suits you best.
If you don't care about the keys, you could use the array_values() function, which will re-index the array starting from zero as I have done in example 5 in Listing 5.
<?php
// Define an array
$wrkAry = array(0=>"Elvis Costello",
1=>"Nick Cave",
2=>"Interpol",
4=>"The Police",
5=>"Sea Pink"
);
// Jump and add an entry
$wrkAry[12] = "Nada Surf";
$wrkAry[] = "Calexico";
$wrkAry = array_values($wrkAry);
// Get array elements and show how many
$loop = count($wrkAry) -1;
echo "Nbr of elements is: $loop <hr>";
// Loop through the using a for loop
for ($i=0; $i<=$loop; $i++) {
echo "The key is: $i / The value is: $wrkAry[$i] <br>";
}
?> Listing 5: Use array_values() to re-index the array.
This will give the following output:
Nbr of elements is: 6
The key is: 0 / The value is: Elvis Costello
The key is: 1 / The value is: Nick Cave
The key is: 2 / The value is: Interpol
The key is: 3 / The value is: The Police
The key is: 4 / The value is: Sea Pink
The key is: 5 / The value is: Nada Surf
The key is: 6 / The value is: Calexico
Let's Summarize
OK, now you know some of the pitfalls you might meet when working with arrays, and you also might have gotten a glimpse of how to avoid them.
The PHP array subject is a huge subject, and I'm only scratching the surface, so if you want to read up before I return with future tips, point your browser to this address and read on:
http://php.net/manual/en/language.types.array.php
You can download the examples here.
Till next time, happy PHPing and RPGing.
LATEST COMMENTS
MC Press Online