AikenDrum Posted October 31, 2009 Share Posted October 31, 2009 Hi there, I have the following need: I have 2 arrays (coming from a databases) Array2 is related to Array1 - Example: Array1 Dog, abc Cat, bcd Horse, cde Array2, meat, abc bones, abc fish, bcd grain, cde grass, cde So, I need the array2 split up into separate arrays - but I am will never know in advance how many rows in both arrays there will be. Basically, I need to display the data so: Dog <ul> <li>meat</li> <li>bones</li> </ul> Cat <ul> <li>fish</li> </ul> Horse <ul> <li>grain</li> <li>grass</li> </ul> As said, the second array will change very often - but will always have the key from the first array Any help very much appreciated. I do have it sort of working - but EVERY entry from array2 appears under every entry from array1 :- here is the code that I have so far written: <?php $fquestions = $record->getRelatedSet('Markt.FFragen'); $questions = $record->getRelatedSet('FFragen.Fragen'); foreach( $fquestions as $fquestion ) { $ffrage = $fquestion->getField('Markt.FFragen::fFrage'); $ffgroup = $fquestion->getField('Markt.FFragen::ffgroup'); $ffrageID = $fquestion->getField('Markt.FFragen::ID'); echo'<h3><a href="#">'.$ffrage.'</a></h3> <div> <ul>'; foreach( $questions as $question ) { $frage = $question->getField('FFragen.Fragen::Frage'); $frageID = $question->getField('FFragen.Fragen::ID'); echo '<li>'.$frage.'<div id="slider"></div></li>'; } echo '</ul> </div>'; } ?> Many thanks in advance AikenD Link to comment https://forums.phpfreaks.com/topic/179696-create-multiple-n-arrays/ Share on other sites More sharing options...
trq Posted October 31, 2009 Share Posted October 31, 2009 Surely this would be best sorted within your database query? Link to comment https://forums.phpfreaks.com/topic/179696-create-multiple-n-arrays/#findComment-948137 Share on other sites More sharing options...
AikenDrum Posted October 31, 2009 Author Share Posted October 31, 2009 Hi there, not fully sure what you mean - The data comes from 2 table - filter questions and questions (ffragen and fragen) Each question has a groupID - each answer also has this groupID - this is where they belong together, I have also tried the following: <?php $fquestions = $record->getRelatedSet('Markt.FFragen'); $questions = $record->getRelatedSet('FFragen.Fragen'); foreach( $fquestions as $fquestion ) { $ffrage = $fquestion->getField('Markt.FFragen::fFrage'); $ffgroup = $fquestion->getField('Markt.FFragen::ffgroup'); $ffrageID = $fquestion->getField('Markt.FFragen::ID'); echo'<h3><a href="#">'.$ffrage.'</a></h3> <div> <ul>'; foreach( $questions as $question ) { $frage = $question->getField('FFragen.Fragen::Frage'); $frageID = $question->getField('FFragen.Fragen::ID'); $fqGroup = $question->getField('FFragen.Fragen::ffGroup'); /* if($ffgroup!=$fqgroup){ unset($frage); continue; } */ echo '<li>'.$frage.', '.$fqGroup.'<div id="slider"></div></li>'; } echo '</ul> </div>'; } ?> But that causes all the questions to disappear Many thanks for your prompt answer AikenD Link to comment https://forums.phpfreaks.com/topic/179696-create-multiple-n-arrays/#findComment-948236 Share on other sites More sharing options...
AikenDrum Posted October 31, 2009 Author Share Posted October 31, 2009 I made a mistake in my naming's (oops) Corrected code = <?php $fquestions = $record->getRelatedSet('Markt.FFragen'); $questions = $record->getRelatedSet('FFragen.Fragen'); foreach( $fquestions as $fquestion ) { $ffrage = $fquestion->getField('Markt.FFragen::fFrage'); $ffGroup = $fquestion->getField('Markt.FFragen::ffGroup'); $ffrageID = $fquestion->getField('Markt.FFragen::ID'); echo'<h3><a href="#">'.$ffrage.'</a></h3> <div> <ul>'; foreach( $questions as $question ) { $frage = $question->getField('FFragen.Fragen::Frage'); $frageID = $question->getField('FFragen.Fragen::ID'); $fqGroup = $question->getField('FFragen.Fragen::ffGroup'); if($fqGroup!=$ffGroup){ unset($frage); continue; } echo '<li>'.$frage.', '.$fqGroup.'<div id="slider"></div></li>'; } echo '</ul> </div>'; } ?> That works ! AikenD Link to comment https://forums.phpfreaks.com/topic/179696-create-multiple-n-arrays/#findComment-948238 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.