gpt4 book ai didi

php - 将对象的数组树转换为具有级别的关联数组

转载 作者:行者123 更新时间:2023-11-29 14:10:33 24 4
gpt4 key购买 nike

我有对象树:

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);
}
}
}

有什么改进建议吗?

最佳答案

我会追求以下内容:

function printArray($arr, &$target, $header = '') {
foreach($arr as $category) {
$target[$category->id] = $header . $category->name;
printArray($category->subcategories, $target, $header . ' ');
}
}

// And then, from your source
$targetArray = array();
printArray($source, $targetArray);

这不会处理可能的冲突,但如果您的数据来自单个表,则不应发生冲突。

要进行更多自定义,您可以将 header 修改作为参数传递给递归函数,例如:

function printArray($arr, &$target, $header = '', $headerIncrement = ' ') {
foreach($arr as $category) {
$target[$category->id] = $header . $category->name;
printArray($category->subcategories, $target, $header . $headerIncrement, $headerIncrement);
}
}

这将允许你调用它

printArray($source, $target, '', '   ');
//or even
printArray($source, $target, '', '<span class="spacer">&nbsp;</span>');

并在 CSS 中添加样式以更改您想要的间距。

关于php - 将对象的数组树转换为具有级别的关联数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13563927/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com