Welcome to WebmasterWorld Guest from 54.159.214.250

Forum Moderators: coopster & jatar k

Explanation of Array Functionality

   
4:14 pm on May 9, 2013 (gmt 0)

10+ Year Member



Hello All,

Before I start I am a complete newbie to PHP and I am trying to further my education. I have been looking into arrays and Have come across this particular line of code.

$font_table = array(array(array('nothing', 'one'),
array('two', 'three')),
array(array('four', 'five'),
array('six', 'seven')));

echo $font_table[!empty($a['Font3'])][!empty($a['Font2'])][!empty($a['Font'])];


From my understanding of arrays this seems to be a multidimensional but what I'm interesting in is the echo response which pinpoints the keys values. you can pin point different keys based upon weather or not a key is empty. I have been scratching my head and tried to look into why this works. I just need a bit of help, or at least an explanation to what is going on. I would be very great full if someone could be kind enough to give me insight into this piece of code.

regards

w9914420
7:52 pm on May 9, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



this seems to be a multidimensional

Yes, it unpacks to (thank you, text editor)
$font_table[0][0][0] = 'nothing'
$font_table[0][0][1] = 'one'
$font_table[0][1][0] = 'two'
$font_table[0][1][1] = 'three'
$font_table[1][0][0] = 'four'
$font_table[1][0][1] = 'five'
$font_table[1][1][0] = 'six'
$font_table[1][1][1] = 'seven'

working outward from
array('two', 'three')
{is the same as}
$blahblah[0] = 'two'
$blahblah[1] = 'three'

The short form obviously saves a lot of space-- but also creates a lot more room for confusion and error.
9:26 pm on May 9, 2013 (gmt 0)

10+ Year Member



lucy24 thank you for making me see the light, all is clear.

regards

w9914420
10:00 pm on May 10, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



When using the short form, you can spare yourself / your peers a few headaches just by spacing it out a bit.

$font_table = array(
array(
array(
'nothing',
'one'
),
array(
'two',
'three'
)
),
array(
array(
'four',
'five'
),
array(
'six',
'seven'
)
)
);
2:31 pm on May 11, 2013 (gmt 0)

10+ Year Member



Hi Readie,
I absolutely agree, now that I understand arrays better.

thanks

w9914420
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month