How can turn multi-dimensional array into series of <ul>?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

How can turn multi-dimensional array into series of <ul>?

Post by someguyhere »

I have an array (see below) that I want to turn into a series of unordered lists, but can't seem to find a foreach structure that works for this situation. Any advice would be greatly appreciated.

Code: Select all

Array (
[0] => Array (
  [0] => <li>Affiliate Networks</li>
  [1] => <li>Copywriting</li>
  [2] => <li>Email Marketing</li>
) 

[1] => Array (
  [0] => <li>Graphic Design</li>
  [1] => <li>Photographers</li>
  [2] => <li>Public Relations</li>
) 

[2] => Array (
  [0] => <li>Video Production</li>
  [1] => <li>Website Design</li>
  [2] => <li>Website Hosting</li>
)
)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How can turn multi-dimensional array into series of <ul>

Post by social_experiment »

Code: Select all

<?php
$me = array(
		array('Affiliate Networks', 'Copywriting', 'Email Marketing'),
		array('Graphic Design', 'Photographers', 'Public Relations'),
		array('Video Production', 'Website Design', 'Website Hosting')
);
	foreach ($me as $value) {
		if (is_array($value)) {
			echo '<ul>';
			foreach ($value as $sub_value ) {
				echo '<li>' . $sub_value . '</li>';
			}
			echo '</ul>';
		}
	}

?>
This turns the multi-dimensional array in 3 smaller lists.
Hth
Last edited by social_experiment on Thu Mar 31, 2011 9:24 am, edited 1 time in total.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

Re: How can turn multi-dimensional array into series of <ul>

Post by MindOverBody »

Code: Select all

$finalOutput = "";
foreach( $array as $list ){
     $output = "<ul>";
     foreach( $list as $item ) $output .= $item;
     $output .= "</ul>";
     $finalOutput .= $output;
}
$array is your targeted array, and $finalOutput is string of unordered lists.

Cheers!
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: How can turn multi-dimensional array into series of <ul>

Post by someguyhere »

MindOverBody wrote:

Code: Select all

$finalOutput = "";
foreach( $array as $list ){
     $output = "<ul>";
     foreach( $list as $item ) $output .= $item;
     $output .= "</ul>";
     $finalOutput .= $output;
}
$array is your targeted array, and $finalOutput is string of unordered lists.

Cheers!
It worked perfectly, though the first line isn't needed.

Thanks!
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How can turn multi-dimensional array into series of <ul>

Post by McInfo »

someguyhere wrote:It worked perfectly, though the first line isn't needed.
The first line is necessary to prevent an "undefined variable" notice on the sixth line.

A better solution might be a recursive function to handle lists of any depth.

HTML entity encoding of the list items would be prudent.
Post Reply