私はオブジェクトのツリーを持っています:
array(4) (
0 => object stdClass(14) {
public id => string(1) "1"
public parent_id => string(1) "0"
public name => string(18) "Stationary engines"
public uri => string(18) "stationary-engines"
public created => string(19) "2012-11-19 15:15:34"
public updated => NULL
public subcategories => array(4) (
0 => object stdClass(14) {
public id => string(1) "5"
public parent_id => string(1) "1"
public name => string(6) "Yanmar"
public uri => string(6) "yanmar"
public created => string(19) "2012-11-19 15:23:36"
public updated => NULL
public subcategories => array(1) (
0 => object stdClass(14) {
public id => string(2) "15"
public parent_id => string(1) "5"
public name => string(18) "Yanmar subcategory"
public uri => string(18) "yanmar-subcategory"
public created => string(19) "2012-11-21 16:38:06"
public updated => NULL
public subcategories => array(1) (
0 => object stdClass(13) {
public id => string(2) "16"
public parent_id => string(2) "15"
public name => string(30) "Yanmar subcategory subcategory"
public uri => string(30) "yanmar-subcategory-subcategory"
public created => string(19) "2012-11-21 17:37:00"
public updated => NULL
}
)
}
)
}
1 => object stdClass(13) {
public id => string(1) "6"
public parent_id => string(1) "1"
public name => string(11) "Мercruiser"
public uri => string(10) "mercruiser"
public created => string(19) "2012-11-19 15:23:36"
public updated => NULL
}
2 => object stdClass(13) {
public id => string(1) "7"
public parent_id => string(1) "1"
public name => string(11) "Volvo-Penta"
public uri => string(11) "volvo-penta"
public created => string(19) "2012-11-19 15:24:49"
public updated => NULL
}
3 => object stdClass(13) {
public id => string(1) "8"
public parent_id => string(1) "1"
public name => string(27) "Basic configuration engines"
public uri => string(27) "basic-configuration-engines"
public created => string(19) "2012-11-19 15:24:49"
public updated => NULL
}
)
}
1 => object stdClass(14) {
public id => string(1) "2"
public parent_id => string(1) "0"
public name => string(10) "Generators"
public uri => string(10) "generators"
public created => string(19) "2012-11-19 15:15:58"
public updated => NULL
public subcategories => array(4) (
0 => object stdClass(13) {
public id => string(1) "9"
public parent_id => string(1) "2"
public name => string(6) "Diesel"
public uri => string(6) "diesel"
public created => string(19) "2012-11-19 15:34:38"
public updated => NULL
}
1 => object stdClass(13) {
public id => string(2) "10"
public parent_id => string(1) "2"
public name => string(8) "Gasoline"
public uri => string(8) "gasoline"
public created => string(19) "2012-11-19 15:34:38"
public updated => NULL
}
2 => object stdClass(13) {
public id => string(2) "11"
public parent_id => string(1) "2"
public name => string(6) "Kohler"
public uri => string(6) "kohler"
public created => string(19) "2012-11-19 15:35:35"
public updated => NULL
}
3 => object stdClass(13) {
public id => string(2) "12"
public parent_id => string(1) "2"
public name => string(13) "Fischer Panda"
public uri => string(13) "fischer-panda"
public created => string(19) "2012-11-19 15:36:14"
public updated => NULL
}
)
}
2 => object stdClass(14) {
public id => string(1) "3"
public parent_id => string(1) "0"
public name => string(14) "Boat equipment"
public uri => string(14) "boat-equipment"
public created => string(19) "2012-11-19 15:16:59"
public updated => NULL
public subcategories => array(1) (
0 => object stdClass(13) {
public id => string(2) "13"
public parent_id => string(1) "3"
public name => string(11) "Subcategory"
public uri => string(11) "subcategory"
public created => string(19) "2012-11-19 15:37:12"
public updated => NULL
}
)
}
3 => object stdClass(14) {
public id => string(1) "4"
public parent_id => string(1) "0"
public name => string(11) "Spare parts"
public uri => string(11) "spare-parts"
public created => string(19) "2012-11-19 15:16:59"
public updated => NULL
public subcategories => array(1) (
0 => object stdClass(13) {
public id => string(2) "14"
public parent_id => string(1) "4"
public name => string(11) "Consumables"
public uri => string(11) "consumables"
public created => string(19) "2012-11-19 15:37:39"
public updated => NULL
}
)
}
)
そして、次のように連想配列に変換する必要があります。
array(
'1' => 'category name',
'3' => ' subcategory name',
'4' => ' subcategory name',
'2' => 'category2 name',
'5' => ' subcategory2 name',
);
各サブカテゴリは、スペースでインデントする必要があります。しかし、私は再帰にあまり詳しくなく、助けが必要です。助けていただければ幸いです。前もって感謝します。
更新:私は次のようになりました:
$options = array();
$this->to_array($tree, $options, ' ', 0);
function to_array($tree, & $target, $indention = '', $lvl = 0)
{
foreach($tree as $leaf)
{
$target[$leaf->id] = str_repeat($indention, $lvl).$leaf->name;
if (isset($leaf->subcategories))
{
$this->to_array($leaf->subcategories, $target, $indention, $lvl + 1);
}
}
}
改善に関する提案はありますか?