Coding PHP

PHP Multidimensional Arrays

I don’t know what it is but I love the data structure of Arrays, honestly they were a bit confusing for me. So far I have completed 3 different courses on PHP Arrays, so many built in functions for them to add/remove/modify the array or elements.

So I was doing a lesson and it came up to a nested array aka multidimensional “arrays inside of arrays” and they wanted you to figure out how to properly print out the correct array[] index to retrieve a “string” from the array. I found it kind of fun because you can fool around with this data structure a bit to make a interesting quiz.

Here is a very simple nested array that prints a “Yeah” or “Nope” as is, it might seem a bit like wtf. The fun was in the use of multidimensional indexes as parameters doing the simple math in a ternary logic true/false string result. Take a look, then try to guess what number in the variable string “$xN” is the “Yeah” number.

$xN = [334,500,[224,7,64,[[14,352,74,88,60],66,122,404],380],4,300];
echo ($xN[2][3][0][1] / $xN[3] == $xN[2][3][0][3]) ? "Yeap" : "Nope";

//outputs "Yeap"

/* What number is giving us the "Yeap" true statement from the variable $xN?
Its right in front of your eyes. */

Does this interest you? Read more about Multidimensional Arrays. Look up “php ternary operator” if you like the short logic used in the echo statement.

Leave a Reply

Your email address will not be published. Required fields are marked *

2 + 3 =