saviomf Posted February 24, 2010 Share Posted February 24, 2010 Hi everyone, I am very new at PHP and programming. I have a multidimensional array and a recursive function to traverse through it which is declared as follows: <?php $families = array ( "Griffin"=>array ( "Peter", "Lois", "Megan" ), "Quagmire"=>array ( "Glenn" ), "Brown"=>array ( "Cleveland", "Loretta", "Junior" ) ); //This recursive function will traverse through these arrays: function traverseArray($array) { // Loops through each element. If element again is array, function is recalled. If not, result is echoed. foreach($array as $key=>$value) { if(is_array($value)) { traverseArray($value); }else{ echo " key > ".$key." | value > ".$value."<br />\n"; } } } traverseArray($families); I wish to display the main records/ roots (Griffin, Quagmire, Brows ...) as separate records with their child-arrays like this (I don't want to use the print_r function, I just want the values so that I can display them neatly in HTML). Record 1: Name = Griffin Student 1 = Peter Student 2 = Lois Student 3 = Megan Record 2: Name = Quagmire Student 1 = Glenn Record 2: Name = Brown Student 1 = Cleveland Student 2 = Loretta Student 3 = Junior Please could some one tell me how I could go about doing this? The recursive function just prints out everything in the array and I can't format it. Any help will be greatly appreciated. Cheers Quote Link to comment Share on other sites More sharing options...
Goat Posted February 24, 2010 Share Posted February 24, 2010 Well if the whole system follows the pattern you outlined (record names are keys and student names are in 2nd level of arrays) then I don't really see the need of complicated recursion at all. try this: $families = array ( "Griffin" =>array("Peter", "Lois", "Megan"), "Quagmire" =>array("Glenn"), "Brown" =>array( "Cleveland", "Loretta", "Junior" ) ); function list_families($arr) { foreach($arr as $key=>$value) { echo "Record:".$key; foreach($value as $student) { echo "Student: ".$student; } } } list_families($families); Goat Quote Link to comment Share on other sites More sharing options...
saviomf Posted February 24, 2010 Author Share Posted February 24, 2010 Hi! Thank you for your reply. But what if the child arrays had further arrays under them? Which is why I was using the recursive function. Any ideas? Thanks in advance! Quote Link to comment Share on other sites More sharing options...
Goat Posted February 24, 2010 Share Posted February 24, 2010 Hi! Thank you for your reply. But what if the child arrays had further arrays under them? Which is why I was using the recursive function. Any ideas? Thanks in advance! I see what you mean, but what is exactly what you want? What should a 'child' of student be called? If array is seamless, using recursion of some kind is not a problem but it is obvious from your example that you want different levels of array to use different methods of iteration. Your top array has key-value pair (keys for record names, and values are subarays), And your 2nd level arrays is keyless and values are student names. regards, Goat. Quote Link to comment Share on other sites More sharing options...
saviomf Posted February 24, 2010 Author Share Posted February 24, 2010 Hi Goat, thanks again for the reply. The structure for the array is something like this: root 0 sub root 0 sub root 1 sub root 2 sub sub root 0 sub sub root 1 sub sub sub root 0 * sub sub sub root 1 .... so on and so forth sub sub sub root 2 sub sub root 2 sub root 3 sub root 4 root 1 root 2 root 3 sub root 0 sub sub root 0 sub sub root 1 sub sub sub root 0 sub sub sub root 1 sub sub root 2 sub root 1 root 4 etc etc So, there's pretty much 3- 5 levels of sub arrays. I want to display each root as a record. Also, suppose I want to get the value of sub sub sub root 0 of root 0 (marked with a *) any suggestion on how I would go about it? I think I haven't got any more hair to pull out! Quote Link to comment Share on other sites More sharing options...
Goat Posted February 24, 2010 Share Posted February 24, 2010 Try this then: <?php $families = array ( "Griffin" =>array("Peter", "Lois", "Megan"), "Quagmire" =>array("Glenn" => array("Hannibal", "Henrietta", "Maria" => array( "Donald", "Herbert", "Daniel" )) ), "Brown" =>array( "Cleveland", "Loretta", "Junior" => array("Matthev", "John", "Elenore") ) ); function list_families($arr, $level) { $level_str = ""; for($a=0;$a<$level;$a++) // this is to 'raise' output to proper level { $level_str .= "+"; } foreach($arr as $key => $value) { echo "<br>"; echo $level_str; if(is_array($value)) { echo $key; list_families($value, $level + 1); } else { echo $value; } } } list_families($families, 0); ?> (send me a link to a site when you are done if you want, I am kind of interested what is this going to be) Goat Quote Link to comment Share on other sites More sharing options...
saviomf Posted February 24, 2010 Author Share Posted February 24, 2010 Hi Goat, Thanks for that! I appreciate that you have taken time out to help a complete stranger. Unfortunately, I'm back to square one with regards to having access to the data in the array. It displays perfectly with the levels, but what I'm looking for is to have access to each row and the data store in it. As in my previous structure, what if sub sub sub root 0 contained a address for a hyperlink? I wish to display each row in a div like div class = "row" for each of the rows if that makes sense. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 24, 2010 Share Posted February 24, 2010 For what you are wanting to do I would consider XML HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
saviomf Posted February 24, 2010 Author Share Posted February 24, 2010 hI teamatomic, funny you should say that because I have got the data out of an xml service. I've attached a file to show you a snippet of the real array. Perhaps that will give a clear understanding of the problem. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 24, 2010 Share Posted February 24, 2010 OK. I see a bunch of parent/child/child/etc. Why are you doing anything but treating it as XML in php? Run it through DOM or simpleXML. I would suggest starting out with simpleXML and seeing if it will do what you want. HTH Teamtomic Quote Link to comment Share on other sites More sharing options...
Goat Posted February 24, 2010 Share Posted February 24, 2010 I see that you have various keys there, and that data type is determined solely by key. You can start with mine first example and use bunch of if statements, something like if($key == ''arrAccomodationUnits"){ yadda yadda} to create custom action for every key type. I don't think you are going to need recursion, because you don't have arrays within arrays that look like main arrays (like in 'family' example). 'Images' is always 3rd level array. So it's one large foreach($arr as $key=> $val) statement, and within it several if statements (some with foreach statements within them, like 'images' and 'arrAcomodationUnits'). Work from there. regards, Goat Quote Link to comment Share on other sites More sharing options...
saviomf Posted February 25, 2010 Author Share Posted February 25, 2010 Thanks Goat, I'll try it out. Cheers Quote Link to comment 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.