Thanks, feyd. I guess there's no real advantage to using my function approach. Instead I'd first put the incoming arrays into one big array like your $test.
Which leads me to ole's question. See, I'm trying to create a phplinks like system where users could specify whatever sub-categories they want using a symbol (e.g. "=>").
So a user could, for example, specify they want their link to be under "category1=>sub-category1=>sub-sub-category1=>sub-sub-sub-category1". That whole line would be that link's MySQL entry for category.
Get it? That way I get sub-categories on demand instead of having to define only a certain number of levels beforehand.
So what I want to do is have a loop that goes over the category entires. That means each category "line", as shown above, would be exploded (using "=>") into an array inside $test. Then I'd run the foreach and $menu would contain a tree of categories and sub-categories.
If you're in the main page, I would then present just level 0 categories and each of their first three (I think three is enough) level1 categories.
If you're inside a certain levelX category, I would present a link to its parent level(X-1) category, and the first three level(X+1) categories of that certain category.
Here are the problems so far:
1) How do I quickly unset any category that isn't level(X-1) or among the first 3 level(X+1) categories?
2) How do I store any information inside the tree (I have a links' count I want to store for each category)?
3) If I do that, how do I add the count of each sub-category to each parent category?
Putting array values as keys in deeper levels
Moderator: General Moderators
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Problem 2) solved.
I've managed to enter numbers there by doing
As for problem 3), I've opened it its own topic because I've only managed to sum root branches.
I've managed to enter numbers there by doing
Code: Select all
$test = array(
array('level0a', 'level1a', 'level2a', 'level3a', 1),
array('level0a', 'level1a', 2),
array('level0d', 'level1d', 'level2d', 3),
array('level0c', 'level1c', 4),
array('level0a', 'level1a', 'level2e', 5),
array('level0c', 'level1c', 'level2z', 6),
);
$menu = array();
foreach($test as $row)
{
$end = 1;
if (intval($row[count($row)-1]))
$end = 2;
//unset($row['id']);
$row = array_values($row);
$row = array_filter($row);
$t =& $menu;
for($i = 0, $j = count($row)-$end; $i < $j; ++$i) {
if ($end == 2) {
if (!array_key_exists($row[$i], $t) or !is_array($t[$row[$i]]['sub'])) {
$t[$row[$i]]['sub'] = array();
}
$t =& $t[$row[$i]]['sub'];
} else {
if (!array_key_exists($row[$i], $t) or !is_array($t[$row[$i]]))
$t[$row[$i]] = array();
$t =& $t[$row[$i]];
}
}
if ($end == 2) {
if (!array_key_exists($row[$i]['sub'], $t))
$t[$row[$i]]['count'] = $row[count($row)-1];
} else
if (!array_key_exists($row[$i], $t))
$t[$row[$i]] = '#';
}
Outputs:
Array
(
[level0a] => Array
(
[sub] => Array
(
[level1a] => Array
(
[sub] => Array
(
[level2a] => Array
(
[sub] => Array
(
[level3a] => Array
(
[count] => 1
)
)
)
[level2e] => Array
(
[count] => 5
)
)
[count] => 2
)
)
)
[level0d] => Array
(
[sub] => Array
(
[level1d] => Array
(
[sub] => Array
(
[level2d] => Array
(
[count] => 3
)
)
)
)
)
[level0c] => Array
(
[sub] => Array
(
[level1c] => Array
(
[count] => 4
[sub] => Array
(
[level2z] => Array
(
[count] => 6
)
)
)
)
)
)