Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-27-2002, 08:02 AM   PM User | #1
Dalsor
New Coder

 
Join Date: Jul 2002
Location: Florida, USA
Posts: 96
Thanks: 0
Thanked 0 Times in 0 Posts
Dalsor is an unknown quantity at this point
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!
__________________
if ( bad && possible ) happen();
Sig re-written for faster processing...
Dalsor is offline   Reply With Quote
Old 07-27-2002, 11:21 AM   PM User | #2
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,911
Thanks: 5
Thanked 80 Times in 79 Posts
firepages will become famous soon enough
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 />";
}}}
?>
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 07-27-2002, 11:35 AM   PM User | #3
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,911
Thanks: 5
Thanked 80 Times in 79 Posts
firepages will become famous soon enough
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
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 07-27-2002, 07:51 PM   PM User | #4
Dalsor
New Coder

 
Join Date: Jul 2002
Location: Florida, USA
Posts: 96
Thanks: 0
Thanked 0 Times in 0 Posts
Dalsor is an unknown quantity at this point
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 />";
  }}}
?>
__________________
if ( bad && possible ) happen();
Sig re-written for faster processing...
Dalsor is offline   Reply With Quote
Old 07-28-2002, 08:59 AM   PM User | #5
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,911
Thanks: 5
Thanked 80 Times in 79 Posts
firepages will become famous soon enough
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!
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:52 PM.


Advertisement
Log in to turn off these ads.