Jump to content

Multidimensional Arrays problem


saviomf

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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! :(

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

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.