PHP All I want for xmas are Arrays

Soldato
Joined
18 Oct 2003
Posts
19,415
Location
Midlands
Aloha chaps,

I've been abusing some code recently and developed the following:

PHP:
//get course contents actions and titles in 3 simple arrays		
			$coursecontents = array();
			$courseactions = array();
			$coursetitles = array();
			foreach ($mycourses->children as $coursenode) {
				$coursecontents[] = $coursenode->get_content();
			}
			foreach ($mycourses->children as $coursenode) {
				$courseactions[] = $coursenode->action;
			}
			foreach ($mycourses->children as $coursenode) {
				$coursetitles[] = $coursenode->get_title();
			}
			//reverse courses as they come out in the wrong order
			$reversedcontent = array_reverse($coursecontents);
			$reversedactions = array_reverse($courseactions);
			$reversedtitles = array_reverse($coursetitles);
			
			//print_r($reversedcontent);
			//print_r($reversedactions);
			//print_r($reversedtitles);
			
			$i = 0;
			foreach ($mycourses->children as $coursenode) {
				$branch->add($reversedcontent[$i], $reversedactions[$i], $reversedtitles[$i]);
				$i++;
			}

I've naughtily used 3 arrays where no doubt a multi di array would have done, but my issue was at the bottom when adding them to the branch I was unsure on how to access the necessary data.

The 3 arrays contain info for a course, and they are all in order. When using my multi array I could get the content first, but the next item which should have been content was an action.. etc

If someone could show me how to tidy this up that would be great. Oh and I am pulling content out of an array $mycourses, to put into another array to reverse because $mycourses is the biggest multi di array you've ever seen and I can't use reverse_array on it.
 
Cheers Soap, unfortunately I couldn't get your snippet to work and couldn't fathom where it was breaking down either. sizeof is a PHP function, which is the same as count.

The issue I have is that a menu is being created from the master array and all the items are back to front. So I thought, hey reverse the array and it will pull the items through in the right order.. easy! But the array it's working from is huge. So my solution was to pull out the content I wanted into new 1d arrays and reverse those. I needed 3 bits of info $ccontent is the anchor text, $caction is the URL, $ctitle is the full title.
 
Back
Top Bottom