Jump to content

if statement within 2D arrays not working


ceci

Recommended Posts

I have the following code.

$Rockbands = array(
"First Row" => array('Beatles','Love Me Do', 'Hey Jude','Helter Skelter'),
"Second Row" => array('Rolling Stones','Waiting on a Friend','Angie','Yesterday\'s Papers'),
"Third Row" => array('Eagles','Life in the Fast Lane','Hotel California','Best of My Love'),

 

I need to do print each row according to the key using foreach() function. I though the following code would work but it does not.

foreach($Rockbands as $key => $val){
  // echo $key prints prints First Row, Second Row..etc. 
IF($key == "First Row"){
     echo 'display First Row array'; 
}elseif($key == "Second Row")
    echo 'display Second Row array.';
}else{
   echo 'nothing to display'; 
}

 

The code above ignores the if statement and prints:

display First Row array

display Second Row array.

nothing to display

 

How do I achieve this? thanks

 

C

 

 

Well the foreach will loop through the array.

 

The first time through it prints the first line because the $key == "First Row"

The second time through it prints the second line because the $key == "Second Row"

The third time through it prints "nothing to display" because $key == "Third Row"

 

I think you might need to look at using something other than foreach for what you are attempting to accomplish, because your code is doing exactly what you are asking it to do.

 

Yeah I reread that statement and thought "thank you captain obvious"

 

What do you mean by "do print each row according to the key"?

 

Just show me the output that you want and I'll show you the change that you need to make. 

 

Given the example array:

$Rockbands = array(

"First Row" => array('Beatles','Love Me Do', 'Hey Jude','Helter Skelter'),

"Second Row" => array('Rolling Stones','Waiting on a Friend','Angie','Yesterday\'s Papers'),

"Third Row" => array('Eagles','Life in the Fast Lane','Hotel California','Best of My Love'),

 

What do you want the output of the foreach command to print?

Well based on the given array, I would like to print only items from that array, say

foreach($Rockbands as $key => $val){
if ($key = "First Row"){
   echo $val; //print values for this array only. eg. Beatles, Love Me Do, Hey Jude, Helter Skelter 
   // Do not print anything else. 
}elseifif ($key = "Second Row"){
   echo $val; //print values for this array only. eg. Rolling Stones, Waiting on a Friend, Angie, Yesterday 
   // Do not print anything else. 
}
}

 

Because I am inside the foreach() loop  my problem is that it prints everything in sequence.  At first I thought the if statementwould be able to control this but it does not.

 

Maybe I was erroneously thinking that I could do something similar to this but inside the foreach() loop: 

<?php
$d=date("D");
if ($d=="Fri")
  echo "Have a nice weekend!";
elseif ($d=="Sun")
  echo "Have a nice Sunday!";
else
  echo "Have a nice day!";
?>

 

 

I may be going the wrong direction here, but maybe you are looking to nest foreach:

 

foreach($Rockbands as $key => $val){

echo $key.": ";

foreach($args as $val){

echo $val.", ";

}

echo "\n";

}

 

I'm sorry if this isn't what you were looking for.

Your code will print

First Row: Beatles, Love Me Do, Hey Jude, Helter Skelter

Second Row: Rolling Stones, Waiting on a Friend, Angie, Yesterday

etc.

 

I don't want that.

 

 

Looks like instead of  arrays like this

$Rockbands = array(

"First Row" => array('Beatles','Love Me Do', 'Hey Jude','Helter Skelter'),

"Second Row" => array('Rolling Stones','Waiting on a Friend','Angie','Yesterday\'s Papers'),

"Third Row" => array('Eagles','Life in the Fast Lane','Hotel California','Best of My Love'),

 

I might have to create separate arrays for each row so I have more control over it.  Right b/c all the arrays belong to the $Rockbands it doesn't seem possible if place an if statement inside of it to only execute the First Row or the Second Row. It prints all the arrays sequentially which is probably how multi-dimentional arrays is supposed to behave.

 

 

 

If you want more control over a 2D array like variable---  then you want a class.

 

class Rockband {

public $name;

public $songs=array();

 

public __construct($band_name,$song_array)

{

$this->name=$band_name;

$this->songs=$song_array;

}

}

 

Then you do this:

$First=new Rockband("Beatles", array("Love Me Do", "Hey Jude", "Helter Skelter"));

 

then you can do stuff like:

 

echo $First->name. " has the songs: \n";

foreach ($First->song as $song){

echo $song.  ', ";

}

 

You can also make it a function of the class:

 

class Rockband {

public $name;

public $songs=array();

 

public function __construct($band_name,$song_array)

{

$this->name=$band_name;

$this->songs=$song_array;

}

public function printSongs()

{

foreach ($First->song as $song){

echo $song.  ', ";

}

 

}

}

 

 

$First=new Rockband("Beatles", array("Love Me Do", "Hey Jude", "Helter Skelter"));

echo $First->name. " has the songs: \n";

$First->printSongs();

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.