The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.
When the nested while returns false, it fails the parent while loop as well.
Any thoughts?
Looking at the function you've done, without testing, it looks as though its doing the right kind of thing. What isn't it doing that it should be? Is your problem to do with detecting the lowest level?
I need a function to do the following: display the array structure to a given depth. Here is an example of the array:
Array
(
[1] => Array
(
[2] => Array
(
[4] => 0
)
)
[3] => Array
(
[5] => 0
)
)
So, if the function was called with the parameter 1, it would display:
- 1
- 3
If the parameter was 2, it would display:
- 1
- - 2
- 3
- - 5
And 3 would give:
- 1
- - 2
- - - 4
- 3
- - 5
The problem is making a recursive function that could, in theory, handle an array of depth, say, 100. Any thoughts would be very much appreciated.
The best I've managed is this:
function rec ($data, $index, $i, $max) {
if (is_array($index)) {
if ($i<=$max)
{
foreach ($index as $volume => $issue)
{
echo "
\n";";
if ($i==$max) { $i=1; break; }
else { $i++;}
rec($data, $issue, $i, $max);
echo "
$i--;
}
echo "";
}
}
}
rec($data, $index, 1, 3);
-----------
The problem is I need to be able to insert content at the lowest level. Also, my script is a pretty messy hack...
The project I'm working on at the moment is deceptive - sounds simple but isn't because some of the high-level functions you would expect to exist, don't, and you have to go back to basics to get round them.
It's basically a recursive database lookup, where articles are within containers which can either be sub-containers or top-level containers - the depth of sub-containers levels is flexible and undefined though.
Makes the whole thing very complex.
Array
(
[1] => Array
(
[0] => 4
[1] => 2
[2] => 1
)
[2] => Array
(
[0] => 5
[1] => 3
)
)
I have the value, for example, 4, and I want it to return the top level key, e.g. 1.
So, 4, 2 or 1 would return 1, and 5 or 3 would return 2.
I can get it to return the key, but not the top level key... any thoughts?
> It works, but if anyone knows of a better way to do it, please let me
> know!
Yours works so you might consider it the best way already. :)
You could have also done this though
$string = "Volume 5 -||- Issue 1 -||- Article 1";
$path = explode(" -||- ",$string);
$array=array();
$cp=&$array;
foreach($path as $node) {
array_push($cp,$node);
$cp[$node]=array();
$cp=&$cp[$node];
}
Or something close to that, probably needs a little tuning.
Hardly elegant either, probably the best way would have been to create a unique key from your original string but all depends on how you're using the data I suppose.
I solved the first problem just by not using recursive while statements - I found a much easier way to do it with foreach anyway.