Make my loop something recursive
I can't quite get my head around this one. I've got an array like so:
Array
(
[0] => Array
(
[content_id] => 23
[content_level] => 1
[content_left] => 2
[content_right] => 3
[content_type] => link
[content_url] =>
[content_name] => home
[content_link] => a:3:{s:3:"url";s:7:"_link_/";s:6:"target";s:5:"_self";s:5:"title";s:4:"home";}
[date_modified] => 2010-07-16 10:29:44
[content_path] =>
)
[1] => Array
(
[content_id] => 13
[content_level] => 1
[content_left] => 12
[content_right] => 23
[content_type] => page
[content_url] => company
[content_name] => company
[content_link] =>
[date_modified] => 2010-08-13 12:55:44
[content_path] => company
)
[2] => Array
(
[content_id] => 25
[content_level] => 2
[content_left] => 13
[content_right] => 14
[content_type] => page
[content_url] => About-Us
[content_name] => About Us
[content_link] =>
[date_modified] => 2010-08-13 11:13:40
[content_path] => company/About-Us
)
[3] => Array
(
[content_id] => 28
[content_level] => 2
[content_left] => 15
[content_right] => 16
[content_type] => page
[content_url] => Tecnic-Quality
[content_name] => Tecnic Quality
[content_link] =>
[date_modified] => 2010-08-13 12:40:23
[content_path] => company/Tecnic-Quality
)
[4] => Array
(
[content_id] => 29
[content_level] => 2
[content_left] => 17
[content_right] => 18
[content_type] => page
[content_url] => Why-Choose-Tecnic
[content_name] => Why Choose Tecnic
[content_link] =>
[date_modified] => 2010-08-13 12:45:56
[content_path] => company/Why-Choose-Tecnic
)
[5] => Array
(
[content_id] => 30
[content_level] => 2
[content_left] => 19
[content_right] => 20
[content_type] => page
[content_url] => Tecnic-Functionality
[content_name] => Tecnic Functionality
[content_link] =>
[date_modified] => 2010-08-13 12:50:06
[content_path] => company/Tecnic-Functionality
)
[6] => Array
(
[content_id] => 31
[content_level] => 2
[content_left] => 21
[content_right] => 22
[content_type] => page
[content_url] => Customer-Focus
[content_name] => Customer Focus
[content_link] =>
[date_modified] => 2010-08-13 12:55:44
[content_path] => company/Customer-Focus
)
[7] => Array
(
[content_id] => 16
[content_level] => 1
[content_left] => 24
[content_right] => 59
[content_type] => module
[content_url] => products
[content_name] => products
[content_link] =>
[date_modified] => 2010-08-13 14:19:44
[content_path] => products
)
[8] => Array
(
[content_id] => 17
[content_level] => 2
[content_left] => 25
[content_right] => 32
[content_type] => module
[content_url] => SkyMax-Series
[content_name] => SkyMax Series
[content_link] =>
[date_modified] => 2010-08-13 13:24:18
[content_path] => products/SkyMax-Series
)
[9] => Array
(
[content_id] => 0
[content_name] => SkyMax Patio Retractable Roof System
[content_path] => products/SkyMax-Series/SkyMax-Patio-Retractable-Roof-System-1281674658
[content_item] => 1
[content_level] => 3
[content_left] => 25
[content_right] => 32
[content_type] => page
[date_modified] => 2010-08-13 15:14:36
)
[10] => Array
(
[content_id] => 0
[content_name] => SkyMax Fleetwood Retractable Roof system
[content_path] => products/SkyMax-Series/skyMax-fleetwood-Retractable-Roof-system
[content_item] => 1
[content_level] => 3
[content_left] => 25
[content_right] => 32
[content_type] => page
[date_modified] => 2010-08-13 15:14:36
)
And I need to loop through it and turn it into something prettier based on content_path. I want it to be a multidimensional array with the element "item" containing the content_id,name,path etc and the rest of the elements relating to other menu items.
This is my code:
foreach($menu as $key => $item)
{
$parts = explode('/',$item['content_path']);
switch (count($parts))
{
case 3:
$ul[$parts[0]][$parts[1]][$parts[2]]['item'] = $item;
break;
case 2:
$ul[$parts[0]][$parts[1]]['item'] = $item;
break;
case 1:
$ul[$parts[0]]['item'] = $item;
break;
}
}
It works fine, the output being
Array
(
[] => Array
(
[item] => Array
(
[content_id] => 23
[content_level] => 1
[content_left] => 2
[content_right] => 3
[content_type] => link
[content_url] =>
[content_name] => home
[content_link] => a:3:{s:3:"url";s:7:"_link_/";s:6:"target";s:5:"_self";s:5:"title";s:4:"home";}
[date_modified] => 2010-07-16 10:29:44
[content_path] =>
)
)
[company] => Array
(
[item] => Array
(
[content_id] => 13
[content_level] => 1
[content_left] => 12
[content_right] => 23
[content_type] => page
[content_url] => company
[content_name] => company
[content_link] =>
[date_modified] => 2010-08-13 12:55:44
[content_path] => company
)
[About-Us] => Array
(
[item] => Array
(
[content_id] => 25
[content_level] => 2
[content_left] => 13
[content_right] => 14
[content_type] => page
[content_url] => About-Us
[content_name] => About Us
[content_link] =>
[date_modified] => 2010-08-13 11:13:40
[content_path] => company/About-Us
)
)
[Tecnic-Quality] => Array
(
[item] => Array
(
[content_id] => 28
[content_level] => 2
[content_left] => 15
[content_right] => 16
[content_type] => page
[content_url] => Tecnic-Quality
[content_name] => Tecnic Quality
[content_link] =>
[date_modified] => 2010-08-13 12:40:23
[content_path] => company/Tecnic-Quality
)
)
[Why-Choose-Tecnic] => Array
(
[item] => Array
(
[content_id] => 29
[content_level] => 2
[content_left] => 17
[content_right] => 18
[content_type] => page
[content_url] => Why-Choose-Tecnic
[content_name] => Why Choose Tecnic
[content_link] =>
[date_modified] => 2010-08-13 12:45:56
[content_path] => company/Why-Choose-Tecnic
)
)
[Tecnic-Functionality] => Array
(
[item] => Array
(
[content_id] => 30
[content_level] => 2
[content_left] => 19
[content_right] => 20
[content_type] => page
[content_url] => Tecnic-Functionality
[content_name] => Tecnic Functionality
[content_link] =>
[date_modified] => 2010-08-13 12:50:06
[content_path] => company/Tecnic-Functionality
)
)
[Customer-Focus] => Array
(
[item] => Array
(
[content_id] => 31
[content_level] => 2
[content_left] => 21
[content_right] => 22
[content_type] => page
[content_url] => Customer-Focus
[content_name] => Customer Focus
[content_link] =>
[date_modified] => 2010-08-13 12:55:44
[content_path] => company/Customer-Focus
)
)
)
[products] => Array
(
[item] => Array
(
[content_id] => 16
[content_level] => 1
[content_left] => 24
[content_right] => 59
[content_type] => module
[content_url] => products
[content_name] => products
[content_link] =>
[date_modified] => 2010-08-13 14:19:44
[content_path] => products
)
[SkyMax-Series] => Array
(
[item] => A开发者_如何学Crray
(
[content_id] => 17
[content_level] => 2
[content_left] => 25
[content_right] => 32
[content_type] => module
[content_url] => SkyMax-Series
[content_name] => SkyMax Series
[content_link] =>
[date_modified] => 2010-08-13 13:24:18
[content_path] => products/SkyMax-Series
)
[SkyMax-Patio-Retractable-Roof-System-1281674658] => Array
(
[item] => Array
(
[content_id] => 0
[content_name] => SkyMax Patio Retractable Roof System
[content_path] => products/SkyMax-Series/SkyMax-Patio-Retractable-Roof-System-1281674658
[content_item] => 1
[content_level] => 3
[content_left] => 25
[content_right] => 32
[content_type] => page
[date_modified] => 2010-08-13 15:14:36
)
)
But it won't work with any more then 3 levels for obvious reasons. Anyone got some ideas on making this recursive? maybe using a function that calls itself perhaps?
A recursive function isn't necessary, just a for
loop will do:
$parts = explode('/',$item['content_path']);
$array = &$ul;
for ($i = 0; $i < count($parts); $i++)
$array = &$array[$parts[$i]];
$array['item'] = $item;
This loops over $parts
, with each iteration obtaining a reference to the next dimension of $ul
.
精彩评论