Jump to content

stuck on foreach


mraza

Recommended Posts

hi all i am learning PHP and trying to run an multidimensional array loop.. i am stuck on one place please somebody can help where i am wrong...thanks and here is the code:

 

$movies = array (
			array( "Gener"=>"Action",
							array (	"Name"=>"Pirates of Carebian",
									"Gener"=>"Action",
									"Year"=>2003,
									"Cast"=>"Johnny Depp"
									),
							array (	"Name"=>"Matrix",
									"Gener"=>"Action",
									"Year"=>2001,
									"Cast"=>"I don't know"
									)
				 ),
			array( "Gener1"=>"Horror",
							array( 	"Name"=>"The Eye",
									"Gener"=>"Horror",
									"Year"=>2002,
									"Cast"=>"Some creepy people"
									),
							array( 	"Name"=>"The Eye",
									"Gener"=>"Horror",
									"Year"=>2002,
									"Cast"=>"Some creepy people"
									)
				),
			array( "Gener2"=>"Romance",
							array( 	"Name"=>"Titanic",
									"Gener"=>"Romance",
									"Year"=>2003,
									"Cast"=>"Some creepy people"
									),
							array( 	"Name"=>"Love",
									"Gener"=>"Romance",
									"Year"=>2008,
									"Cast"=>"Wierd things happen"
									)
				)
			);
foreach ($movies as $arey) {
			foreach( $arey as $list=>$term) {
					foreach ($term as $name=>$detail) {
								echo "$name = $detail<br />";
							};
						};
					};

there is problem in this line " foreach ($term as $name=>$detail) {"

Warning: Invalid argument supplied for foreach() in C:\Program Files\wamp\www\test\index.php on line 149

Link to comment
Share on other sites

I think you were trying to dig to deep.

 <?php
$movies = array (
array( "Gener"=>"Action",
array ( "Name"=>"Pirates of Carebian",
"Gener"=>"Action",
"Year"=>2003,
"Cast"=>"Johnny Depp"
),
array ( "Name"=>"Matrix",
"Gener"=>"Action",
"Year"=>2001,
"Cast"=>"I don't know"
)
),
array( "Gener1"=>"Horror",
array( "Name"=>"The Eye",
"Gener"=>"Horror",
"Year"=>2002,
"Cast"=>"Some creepy people"
),
array( "Name"=>"The Eye",
"Gener"=>"Horror",
"Year"=>2002,
"Cast"=>"Some creepy people"
)
),
array( "Gener2"=>"Romance",
array( "Name"=>"Titanic",
"Gener"=>"Romance",
"Year"=>2003,
"Cast"=>"Some creepy people"
),
array( "Name"=>"Love",
"Gener"=>"Romance",
"Year"=>2008,
"Cast"=>"Wierd things happen"
)
)
);
foreach ($movies as $arey) {
foreach( $arey as $list=>$term) {
foreach ($term as $detail) {
echo $term.' = '.$detail.'<br />';
};
};
};
?>

Link to comment
Share on other sites

<?php

$movies = array
(
    array
    (
        'Gener'=>'Action',
        array (
            'Name'=>'Pirates of Carebian',
            'Gener'=>'Action',
            'Year'=>2003,
            'Cast'=>'Johnny Depp'
        ),
        array (
            'Name'=>'Matrix',
            'Gener'=>'Action',
            'Year'=>2001,
            'Cast'=>'I don\'t know'
        )
    ),
    array
    (
        'Gener'=>'Horror',
        array(
            'Name'=>'The Eye',
            'Gener'=>'Horror',
            'Year'=>2002,
            'Cast'=>'Some creepy people'
        ),
        array(
            'Name'=>'The Eye',
            'Gener'=>'Horror',
            'Year'=>2002,
            'Cast'=>'Some creepy people'
        )
    ),
    array
    (
        'Gener'=>'Romance',
        array(
            'Name'=>'Titanic',
            'Gener'=>'Romance',
            'Year'=>2003,
            'Cast'=>'Some creepy people'
        ),
        array(
            'Name'=>'Love',
            'Gener'=>'Romance',
            'Year'=>2008,
            'Cast'=>'Wierd things happen'
        )
    )
);

foreach ($movies as $movie) { // 0 => array ( "Genre" => "Action", array ( movie ), array ( movie ) )
    $genre = $movie['Gener'];
    print "<h2>$genre</h2>";
    print '<table>';
    print '<tr>';
    print '    <th>Name</th>';
    print '    <th>Gener</th>';
    print '    <th>Release Year</th>';
    print '    <th>Cast</th>';
    print '</tr>';
    $sizeof = sizeof($movie) - 1;
    for ($i = 0; $i < $sizeof; ++$i) {
    print '<tr>';
        $movieName = $movie[$i]['Name'];
        $movieGenre = $movie[$i]['Gener'];
        $movieReleaseYear = $movie[$i]['Year'];
        $movieCast = $movie[$i]['Cast'];
        print "<td>$movieName</td>";
        print "<td>$movieGenre</td>";
        print "<td>$movieReleaseYear</td>";
        print "<td>$movieCast</td>";
    print '</tr>';
    }
    print '</table>';
}

 

However if you are creating data structures like that then you may want to step away from array's and start looking at object oriented programming (OOP)

 

class Actor {}
class ActorCollection {
    protected $_actors;
    public function addActor(Actor $actor) {..}
}
class Movie {
    protected $_name;
    protected $_genre;
    protected $_releaseYear;
    protected $_cast;
    
    public function __construct($name, $releaseYear, ActorCollection $cast) {
         ..
    }
}
class MovieGenre {
}
class MovieCollection {
    protected $_movies; // array ( Genre => array ( Movies ) )
    public function addMovie(Movie $movie) {..}
    public function findByGenre($genre) {..}
}

print new MovieCollection(new MovieCollectionDbAdapter('SELECT * FROM movies'));
// which would output something like:
// Genre
// Name Release Year ..
// Genre
// Name Release Year ..
// ..

 

This is a very incomplete version of what you actually need but it gives you a good idea of what it allows you to do.

Link to comment
Share on other sites

thanks WolfRage for your reply but i got again same error Warning: Invalid argument supplied for foreach() AND @ignace thanks for your post but what i want is to correct that code only..it will show the gener on top...nothing else thanks

Link to comment
Share on other sites

thanks WolfRage for your reply but i got again same error Warning: Invalid argument supplied for foreach() AND @ignace thanks for your post but what i want is to correct that code only..it will show the gener on top...nothing else thanks

 

It does that

 

Genre

..Movie Titles..

Genre2

..Movie Titels..

Genre3

..Movie Titles..

Link to comment
Share on other sites

Thanks ignace but i have just started learning PHP and i am on learning arrays that how they works so i want's that information for this time if you can help thanks :)

i am attaching the picture what i am getting:

i just want to show "Gener on this line'

Warning: Invalid argument supplied for foreach() in C:\Program Files\wamp\www\test\index.php on line 149

 

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

the reason for the error is because the first item of the second array is a string,

So it will not work in an foreach.. to solve this you can just add a if statement, (here's an example)

 

<?php
foreach ($movies as $arey){
foreach( $arey as $list=>$term){
	if(is_array($term)){ //if array do foreach
		foreach ($term as $name=>$detail){
			echo "$name = $detail<br />";
		}
	}else{ //if not array then assume its a string and echo it
		echo "<strong>$term</strong><br />";
	}
}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.