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.
 
Not got my coding hat on today but I can see you're looping through the array 3 times which should just be one loop. However, it seems a shame that you cant reverse the master array as that would make the whole thing a lot simpler. So what about something like this?

PHP:
// loop backwards through the array...
for($i = size_of($mycourses) - 1; $i >= 0; $i--) {
    $child = $mycourses[$i];
    $branch->add($child->get_content(), $child->action, $child->get_title());
}

I would say that I dont know what $mycourses is, I assume its an array. Also, I havent done any PHP in a while so size_of might not be correct...
 
Not sure what the desired effect is here. Perhaps a little clearer if you could show some examples of the arrays/objects and what you wish them to look like?

size_of() = count() presumably :)
 
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.
 
I think Soap's code is almost there, but count($mycourses) would break down if $mycourses is an object which holds a children array - you need to count and access the children array.

PHP:
for($i = count($mycourses->children) - 1; $i >= 0; $i--) {
    $child = $mycourses->children[$i]; // not sure you can stack up like this in php, think you might need $children = $mycourse->children; $child = $children[$i];
    $branch->add($child->get_content(), $child->action, $child->get_title());
}
 
Back
Top Bottom