Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Referencing second dimension of 2d array

1. ## Referencing second dimension of 2d array

The answer to this may be right under my nose, but to save me, I can't seem to see it. I currently have a one dimensional array which I would like to expand into a 2d array.

\$warArray[0][] = 'Blah0', NULL;
\$warArray[1][] = 'Blah1', NULL;
\$warArray[2][] = 'Blah2', 'Data';

For the life of me, I can't figure out how to loop through the \$warArray using the 1st dim, and return the 2nd dim if the 2nd dim's value isn't a null string.

Hopefully this makes sense. Been kicking the code so long I don't think it will, though. Something like...

PHP Code:
for ( \$x 0\$x \$maxArrayLength\$x++ )
{
if (
\$warArray[\$x]->[2ndDimension] != NULL )
echo
\$warArray[\$x]->[2ndDimension];

I don't think I'm using the NULL comparison correctly, but it's the closest thing to a C equivalant I can think of. If string[0]->[] is 0x0. I'm pretty sure the pointer reference isn't valid, either, but again, it's the closest thing I can think of. I'm trying to find if the 2nd dimension contains actual data so it can be printed.

Someone once told me PHP was a lot like C, to which I responded hey, it should be easy to learn!

• Hi , I am a bit unsure what you mean with your array structure... do you mean

\$var[0][blah1]=array('val1','val2'); etc ??

or just as below ??

<?
\$warArray[0][blah0] = 'something';
\$warArray[1][blah1] = '';
\$warArray[2][blah2] = 'something else';

while(list(\$key,\$var)=each(\$warArray)){
foreach(\$var as \$v){
if(\$v){
echo "\$v<br />";
}else{
echo "\\$warArray[\$key] has no value<br />";
}}}
?>

• or this works if you meant the [][]=array etc

lol - it also depends on what you mean as NULL , NULL or an empty string ? i.e......

PHP Code:
<?
\$warArray
[0][] = array('Blah0''');
\$warArray[1][] = array('Blah1'NULL);
\$warArray[2][] = array('Blah2''Data');

while(list(
\$key,\$var)=each(\$warArray)){
foreach(
\$var as \$v){
if(!
is_null(\$v[1])){
echo
"2nd dimension of \\$warArray[\$key][] is \$v[1]<br />";
}else{
echo
"2nd dimension of \\$warArray[\$key][] has no  value<br />";
}}}
?>
note how only the NULL array item shows as 'empty', but change
(!is_null(\$v[1])){
for
(\$v)){ //same as 'isset(\$v)'
and the first 2 arrays show up as empty

• You rock, Fire.

This did the job perfectly.

Just to make sure I understand, it's the \$v[1], where 1 refers to the 2nd dimension in the array?

PHP Code:
<?
\$warArray
[0][] = array('Blah0'NULL);
\$warArray[1][] = array('Blah1'NULL);
\$warArray[2][] = array('Blah2''Data');

while(list(
\$key,\$var)=each(\$warArray)){
foreach(
\$var as \$v){
if(!
is_null(\$v[1])){
echo
"2nd dimension of \\$warArray[\$key][] is \$v[1]<br />";
}else{
echo
"2nd dimension of \\$warArray[\$key][] has no  value<br />";
}}}
?>

• yep \$v[0] would be the 'blah\$x' and \$v[1] the value.

Hopefully in Zend2/next major PHP release should have easier and more direct methods of accessing multi-dimensional arrays by treating them as objects, but still waiting on that!

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•