Jump to content

Array Print Out


peterg0123

Recommended Posts

Hi,

 

How can I print this multidimensional array out?  Below is a print_r of the array:

 

Array ( [-- No Parent --] => Array ( [0] => Array ( [category_id] => 1 [category_title] => -- No Parent -- ) ) [Microsoft] => Array ( [0] => Array ( [category_id] => 2 [category_title] => Microsoft ) [1] => Array ( [0] => Array ( [category_id] => 3 [category_title] => Outlook 2007 ) [1] => Array ( [0] => Array ( [category_id] => 7 [category_title] => Errors in Outlook ) ) ) [2] => Array ( [0] => Array ( [category_id] => 6 [category_title] => Microsoft Word 2007 ) ) ) [Maximizer] => Array ( [0] => Array ( [category_id] => 4 [category_title] => Maximizer ) [1] => Array ( [0] => Array ( [category_id] => 5 [category_title] => Server Side ) ) ) )

 

It is basically for a dropdown menu of categories.

 

Thanks in advance

Peter

Link to comment
https://forums.phpfreaks.com/topic/144204-array-print-out/
Share on other sites

Hi,

 

Thanks for the link.  Tried it but it get the following:-

 

The actual Key is -- No Parent --.

---> 0 - Array

The actual Key is Microsoft.

---> 0 - Array

---> 1 - Array

---> 2 - Array

The actual Key is Maximizer.

---> 0 - Array

---> 1 - Array

 

I think I need a recursive function or something?

Peter

Link to comment
https://forums.phpfreaks.com/topic/144204-array-print-out/#findComment-756748
Share on other sites

<?php

function getTopCategories () 
{
return getSubCategories(0); /* top-level categories are just sub categories without a parent id */

}

function getSubCategories($parentId){
   $sql = "SELECT category_id,parent_id,category_title FROM tbl_categories WHERE parent_id = $parentId";
   $rs = mysql_query ($sql);
   $results = (array)NULL;
   while ($row = mysql_fetch_assoc($rs)) 
   {
      $id = $row["category_id"];
      $items = getItems($id);
      $subcategories = getSubCategories($id);
      $results[$row["category_title"]] = (array)NULL;
      
  foreach (array ($items, $subcategories) as $resultset) 
  {
         foreach ($resultset as $result) 
	 {
            $results[$row["category_title"]][] = $result;
         }
      }
   }
   return $results;
}
function getItems ($category_id) 
{
   $sql = "SELECT category_id,category_title FROM tbl_categories WHERE category_id='$category_id'";
   $rs = mysql_query ($sql);
   $results = (array)NULL;
   
   while ($row = mysql_fetch_assoc ($rs)) 
   {
      $results[] = $row;
   }
   return $results;
}


?>

Link to comment
https://forums.phpfreaks.com/topic/144204-array-print-out/#findComment-756752
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.