gpt4 book ai didi

从数组+对象创建嵌套的 ul li 的 php 函数

转载 作者:搜寻专家 更新时间:2023-10-31 20:48:55 24 4
gpt4 key购买 nike

我有这样的数组+对象。

Array
(
[0] => stdClass Object
(
[tid] => 1
[parent_id] => 0
[language] => th
[t_type] => category
[t_name] => cat1
[t_description] =>
[t_uri] => cat1
[t_uri_encoded] => cat1
[t_uris] =>
[meta_title] =>
[meta_description] =>
[meta_keywords] =>
[childs] => Array
(
[0] => stdClass Object
(
[tid] => 3
[parent_id] => 1
[language] => th
[t_type] => category
[t_name] => cat1.1
[t_description] =>
[t_uri] => cat1.1
[t_uri_encoded] => cat1.1
[t_uris] =>
[meta_title] =>
[meta_description] =>
[meta_keywords] =>
[childs] => Array
(
[0] => stdClass Object
(
[tid] => 5
[parent_id] => 3
[language] => th
[t_type] => category
[t_name] => cat1.1.1
[t_description] =>
[t_uri] => cat1.1.1
[t_uri_encoded] => cat1.1.1
[t_uris] =>
[meta_title] =>
[meta_description] =>
[meta_keywords] =>
)

)

)

[1] => stdClass Object
(
[tid] => 4
[parent_id] => 1
[language] => th
[t_type] => category
[t_name] => cat1.2
[t_description] =>
[t_uri] => cat1.2
[t_uri_encoded] => cat1.2
[t_uris] =>
[meta_title] =>
[meta_description] =>
[meta_keywords] =>
)

)

)

[1] => stdClass Object
(
[tid] => 2
[parent_id] => 0
[language] => th
[t_type] => category
[t_name] => cat2
[t_description] =>
[t_uri] => cat2
[t_uri_encoded] => cat2
[t_uris] =>
[meta_title] =>
[meta_description] =>
[meta_keywords] =>
)

)

这个数组+json字符串中的对象

[{"tid":"1","parent_id":"0","language":"th","t_type":"category","t_name":"cat1","t_description":null,"t_uri":"cat1","t_uri_encoded":"cat1","t_uris":null,"meta_title":null,"meta_description":null,"meta_keywords":null,"childs":[{"tid":"3","parent_id":"1","language":"th","t_type":"category","t_name":"cat1.1","t_description":null,"t_uri":"cat1.1","t_uri_encoded":"cat1.1","t_uris":null,"meta_title":null,"meta_description":null,"meta_keywords":null,"childs":[{"tid":"5","parent_id":"3","language":"th","t_type":"category","t_name":"cat1.1.1","t_description":null,"t_uri":"cat1.1.1","t_uri_encoded":"cat1.1.1","t_uris":null,"meta_title":null,"meta_description":null,"meta_keywords":null}]},{"tid":"4","parent_id":"1","language":"th","t_type":"category","t_name":"cat1.2","t_description":null,"t_uri":"cat1.2","t_uri_encoded":"cat1.2","t_uris":null,"meta_title":null,"meta_description":null,"meta_keywords":null}]},{"tid":"2","parent_id":"0","language":"th","t_type":"category","t_name":"cat2","t_description":null,"t_uri":"cat2","t_uri_encoded":"cat2","t_uris":null,"meta_title":null,"meta_description":null,"meta_keywords":null}]

如何像这样创建嵌套的 ul li。

<ul>
<li>cat1
<ul>
<li>cat1.1
<ul>
<li>cat1.1.1</li>
</ul>
</li>
<li>cat1.2</li>
</ul>
</li>
<li>cat2</li>
</ul>

这是我试过的

echo list_nested_cat( $list_item );
function list_nested_cat( $list_item, $start = 0) {
echo '<ul style="margin-left: 1em;">';
foreach ( $list_item as $item ) {
if ( isset( $item->childs ) && is_array( $item->childs ) ) {
echo '<li>'.$item->t_name.'</li>';
list_nested_cat($item->childs);
} else {
echo '<li>'.$item->t_name.'</li>';
}

}
echo '</ul>';
}

但是这个函数回显了不正确的 html 嵌套 ul li 格式。

附言。我从 arnaud576875 的代码创建数组对象

PHP tree structure for categories and sub categories without looping a query

最佳答案

这应该让你开始:

它使用递归函数;它会调用自己。

已更新

使用 JSON 对象进行测试并修复了一些错误

<?php    

echo make_ulli($object);

function make_ulli($array){
if(!is_array($array)) return '';

$output = '<ul>';
foreach($array as $item){

$output .= '<li>' . $item->t_name;

if(property_exists($item, 'childs'))
$output .= make_ulli($item->childs);

$output .= '</li>';

}
$output .= '</ul>';

return $output;
}


?>

关于从数组+对象创建嵌套的 ul li 的 php 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10309006/

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