Forum Moderators: coopster

Message Too Old, No Replies

display multiple array newbie help

php multiple array displa

         

pixel_juice

7:58 pm on Oct 31, 2005 (gmt 0)

10+ Year Member



One thing I've never been able to get my head around properly is arrays. I can make them just fine ;) but sorting and displaying them usually causes me great frustration.

I have an array like this:

Array
(
[stock] => Array
(
[farm] => Array
(
[chicken] => 3
[sheep] => 3
[owls] => 1
)

[domestic] => Array
(
[dog] => 5
[cat] => 3
)

[made_up] => Array
(
[unicorn] => 5
[batman] => 3
)
)

)

All I need to do is output something like this:


farm
chicken 3
sheep 3
owls 1

domestic
dog 5

etc...

At the moment the more I read the more confused I seem to become. The multiple arrays is what seems to be my problem although I know the answer must be extremely simple. Can anyone point me in the right direction before I lose any more hair ;)

StupidScript

8:10 pm on Nov 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I wouldn't say that my example is extremely simple, but hopefully it is transparent enough to help you see what's going on in your arrays.

<?

$inventory = array (

 "stock" => array (

 "farm" => array ( "chicken" => 3, "sheep" => 3, "owls" => 1 ),

 "domestic" => array ( "dog" => 5, "cat" => 3 ),

 "made_up" => array ( "unicorn" => 5, "batman" => 3 ),

 "manager" => "Suzie"

 ),

 "tools" => array (

 "farm" => array ( "tractor" => 3, "truck" => 3, "combine" => 1 ),

 "domestic" => array ( "stove" => 5, "blender" => 3 ),

 "made_up" => array ( "finwhistle" => 5, "tillman" => 3 ),

 "manager" => "Steve"

 )

);

[/code]
[code]# SORT ON THIS CATEGORY

$category_match = "stock";

# AND SUBCATEGORY

$subcategory_match = "farm";

[/code]
[code]foreach($inventory as $category => $catarray) {

 # COMMENT NEXT LINE IF NOT SORTING ON CATEGORY

 if ($category===$category_match) {

 echo "CATEGORY: ".$category."<br />\n";

 if (is_array($catarray)) {

  foreach($catarray as $subcategory => $subarray) {

  if (is_array($subarray)) {

   # COMMENT NEXT LINE IF NOT SORTING ON SUBCATEGORY

   if ($subcategory===$subcategory_match) {

   echo "SUBCATEGORY: ".$subcategory."<br />\n";

   foreach($subarray as $item => $value) {

    echo "ITEM: ".$item." => ".$value."<br />\n";

   }

   # COMMENT NEXT LINE IF NOT SORTING ON SUBCATEGORY

   }

  }

  else {

   echo "ITEM: ".$subcategory." => ".$subarray."<br />\n";

  }

  }

 }

 else {

  echo "SUBCATEGORY: ".$category." => ".$catarray."<br />\n";

 }

 # COMMENT NEXT LINE IF NOT SORTING ON CATEGORY

 }

}

?>

Maybe ...?

StupidScript

9:28 pm on Nov 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<
MODERATOR:
Please feel free to delete my previous post. This is substantially the same, but with a couple of extras and comments.
>

I wouldn't say that my example is extremely simple, but hopefully it is transparent enough to help you see what's going on in your arrays. I added a "printed" element, added an extra subarray (tools) and added an extra element to each subarray that is a simple name-value pair.

<?

$inventory = array (

 "printed" => date("M. d, Y"),

 "stock" => array (

 "farm" => array ( "chicken" => 3, "sheep" => 3, "owls" => 1 ),

 "domestic" => array ( "dog" => 5, "cat" => 3 ),

 "made_up" => array ( "unicorn" => 5, "batman" => 3 ),

 "manager" => "Suzie"

 ),

 "tools" => array (

 "farm" => array ( "tractor" => 3, "truck" => 3, "combine" => 1 ),

 "domestic" => array ( "stove" => 5, "blender" => 3 ),

 "made_up" => array ( "finwhistle" => 5, "tillman" => 3 ),

 "manager" => "Steve"

 )

);

[/code]
[code]# SORT ON THIS CATEGORY

$category_match = "stock";

# AND SUBCATEGORY

$subcategory_match = "farm";

# THESE CAN BE PASSED VARIABLES, i.e. FROM A FORM

[/code]
[code]# RUN THROUGH MAIN ARRAY ENTRIES

foreach($inventory as $category_name => $category_value) {

 # GRAB AND PRINT THE "PRINTED" VALUE, for fun ... ;)

 if ($category_name==="printed") {

 echo "<strong>PRINTED: ".$category_value."</strong><br />\n";

 }

 # COMMENT NEXT LINE IF NOT SORTING ON CATEGORY

 if ($category_name===$category_match) {

 # PRINT CATEGORY NAME

 echo "CATEGORY: ".$category_name."<br />\n";

 # IS IT AN ARRAY?

 if (is_array($category_value)) {

 # RUN THROUGH CATEGORY ARRAY ENTRIES

 foreach($category_value as $subcategory_name => $subcategory_value) {

 # IS IT AN ARRAY?

 if (is_array($subcategory_value)) {

 # COMMENT NEXT LINE IF NOT SORTING ON SUBCATEGORY

 if ($subcategory_name===$subcategory_match) {

 # PRINT SUBCATEGORY NAME

 echo "SUBCATEGORY: ".$subcategory_name."<br />\n";

 # RUN THROUGH SUBCATEGORY ARRAY ENTRIES

 foreach($subcategory_value as $item => $value) {

 # PRINT ITEM NAMES AND VALUES

 echo "ITEM: ".$item." => ".$value."<br />\n";

 }

 # COMMENT NEXT LINE IF NOT SORTING ON SUBCATEGORY

 }

 }

 else {

 # SUBCATEGORY IS NOT AN ARRAY, SO PRINT IT

 echo "ITEM: ".$subcategory_name." => ".$subcategory_value."<br />\n";

 }

 }

 }

 else {

 # CATEGORY IS NOT AN ARRAY, SO PRINT IT

 echo "SUBCATEGORY: ".$category_name." => ".$category_value."<br />\n";

 }

 # COMMENT NEXT LINE IF NOT SORTING ON CATEGORY

 }

}

?>

Whew.

pixel_juice

8:22 pm on Nov 12, 2005 (gmt 0)

10+ Year Member



Thanks for the help Stupidscript. I think I learned something from it, although as far as I can tell I've ended with a bit of a kludgy workaround ;)