mraza Posted July 17, 2009 Share Posted July 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/166332-stuck-on-foreach/ Share on other sites More sharing options...
WolfRage Posted July 17, 2009 Share Posted July 17, 2009 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 />'; }; }; }; ?> Quote Link to comment https://forums.phpfreaks.com/topic/166332-stuck-on-foreach/#findComment-877107 Share on other sites More sharing options...
ignace Posted July 17, 2009 Share Posted July 17, 2009 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/166332-stuck-on-foreach/#findComment-877109 Share on other sites More sharing options...
mraza Posted July 17, 2009 Author Share Posted July 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/166332-stuck-on-foreach/#findComment-877110 Share on other sites More sharing options...
WolfRage Posted July 17, 2009 Share Posted July 17, 2009 Are you sure that it is that same line? Quote Link to comment https://forums.phpfreaks.com/topic/166332-stuck-on-foreach/#findComment-877116 Share on other sites More sharing options...
mraza Posted July 17, 2009 Author Share Posted July 17, 2009 Are you sure that it is that same line? Yes you can try running on your side Quote Link to comment https://forums.phpfreaks.com/topic/166332-stuck-on-foreach/#findComment-877123 Share on other sites More sharing options...
ignace Posted July 17, 2009 Share Posted July 17, 2009 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.. Quote Link to comment https://forums.phpfreaks.com/topic/166332-stuck-on-foreach/#findComment-877124 Share on other sites More sharing options...
mraza Posted July 17, 2009 Author Share Posted July 17, 2009 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] Quote Link to comment https://forums.phpfreaks.com/topic/166332-stuck-on-foreach/#findComment-877128 Share on other sites More sharing options...
mraza Posted July 18, 2009 Author Share Posted July 18, 2009 anybody help me to solve this issue please Quote Link to comment https://forums.phpfreaks.com/topic/166332-stuck-on-foreach/#findComment-877565 Share on other sites More sharing options...
MadTechie Posted July 18, 2009 Share Posted July 18, 2009 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 />"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/166332-stuck-on-foreach/#findComment-877573 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.