peterg0123 Posted February 7, 2009 Share Posted February 7, 2009 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 More sharing options...
gaza165 Posted February 7, 2009 Share Posted February 7, 2009 i would use a foreach loop on your array, for example <?php $array = array("one","two","three","four"); foreach ($array as &$value) { echo $value."<br/>"; // ehos //one //two //three //four } ?> Link to comment https://forums.phpfreaks.com/topic/144204-array-print-out/#findComment-756739 Share on other sites More sharing options...
peterg0123 Posted February 7, 2009 Author Share Posted February 7, 2009 Hi, Not sure that will work. The array is built dynamically? Any other idea's. Thanks Link to comment https://forums.phpfreaks.com/topic/144204-array-print-out/#findComment-756740 Share on other sites More sharing options...
gaza165 Posted February 7, 2009 Share Posted February 7, 2009 http://www.phpf1.com/tutorial/php-multidimensional-array.html?page=3 this is a usefull link for printing multidimensional arrays Link to comment https://forums.phpfreaks.com/topic/144204-array-print-out/#findComment-756743 Share on other sites More sharing options...
peterg0123 Posted February 7, 2009 Author Share Posted February 7, 2009 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 More sharing options...
gaza165 Posted February 7, 2009 Share Posted February 7, 2009 i dont think ur array is being created properly. how is your array being constructed, copy the code. Link to comment https://forums.phpfreaks.com/topic/144204-array-print-out/#findComment-756750 Share on other sites More sharing options...
peterg0123 Posted February 7, 2009 Author Share Posted February 7, 2009 <?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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.