function test($str) {
$c = count($str);
if ($c>1) {
foreach ($str as $key => $value) $result .= test($value);
} else {
$result = "<li>$str</li>\n";
}
echo $result ? "<ol>$result</ol>" : null;
}
The $str value could be either, something like this;
$str值可以是这样的;
$str1 = "apple";
or something like that;
之类的;
$str2 = array("apple","orange","pear");
If count($str) is more than one, which is $str is is_array, it repeats the $result.
But it doesn't work as I want..
I get an error "Cannot redeclare test() (previously declared in..."
如果count($str)大于1,即$str是is_array,则重复$result。但这并不是我想要的。我得到一个错误“无法重新声明test()”(之前在…中声明的)
The ideal output would be - $str1;
理想的输出是- $str1;
<ol>
<li>apple</li>
</ol>
The ideal output would be - $str2;
理想的输出是- $str2;
<ol>
<li>apple</li>
<li>orange</li>
<li>pear</li>
</ol>
3 个解决方案
#1
1
Check, if $str
is an array, e.g.:
检查,如果$str是一个数组,例如:
function test($str, $first=true) {
if (!is_array($str)) {
$result = "<li>$str</li>\n";
} else {
foreach ($str as $key => $value) $result .= test($value, false);
}
return ($first ? "<ol>$result</ol>" : $result);
}
Also see this example.
也看到这个例子。
=== UPDATE ===
= = = = = =更新
If you want to let it print directly, replace with:
如果你想让它直接打印,可以用:
function test($str, $first=true) {
if (!is_array($str)) $result = "<li>$str</li>";
else foreach ($str as $key => $value) $result .= test($value, false);
if ($first) echo "<ol>$result</ol>";
else return $result;
}
Also see this example.
也看到这个例子。
#2
3
function test($str) {
// Ensure, $str is a good argument of implode()
if ( ! is_array( $str )) {
$str = array( $str );
}
$result = '<li>' . implode( '</li><li>', $str ) . '</li>';
return '<ol>' . $result . '</ol>';
}
Remark: Your method could return this:
注:您的方法可以返回:
<ol>
<li>1</li>
<li>
<ol>
<li>1</li>
<li>2</li>
<li>3</li>
</ol>
</li>
<li>3</li>
</ol>
Does this is really what you want to achieve? Does it really need to create nested OL
structures?
这真的是你想要实现的吗?它真的需要创建嵌套OL结构吗?
#3
1
function testInternal($str){
if(is_array($str))
return implode('',array_map('testInternal', $str));
else
return '<li>'.$str.'</li>';
}
function test($str){
echo '<ol>'.testInternal($str).'</ol>';
}
#1
1
Check, if $str
is an array, e.g.:
检查,如果$str是一个数组,例如:
function test($str, $first=true) {
if (!is_array($str)) {
$result = "<li>$str</li>\n";
} else {
foreach ($str as $key => $value) $result .= test($value, false);
}
return ($first ? "<ol>$result</ol>" : $result);
}
Also see this example.
也看到这个例子。
=== UPDATE ===
= = = = = =更新
If you want to let it print directly, replace with:
如果你想让它直接打印,可以用:
function test($str, $first=true) {
if (!is_array($str)) $result = "<li>$str</li>";
else foreach ($str as $key => $value) $result .= test($value, false);
if ($first) echo "<ol>$result</ol>";
else return $result;
}
Also see this example.
也看到这个例子。
#2
3
function test($str) {
// Ensure, $str is a good argument of implode()
if ( ! is_array( $str )) {
$str = array( $str );
}
$result = '<li>' . implode( '</li><li>', $str ) . '</li>';
return '<ol>' . $result . '</ol>';
}
Remark: Your method could return this:
注:您的方法可以返回:
<ol>
<li>1</li>
<li>
<ol>
<li>1</li>
<li>2</li>
<li>3</li>
</ol>
</li>
<li>3</li>
</ol>
Does this is really what you want to achieve? Does it really need to create nested OL
structures?
这真的是你想要实现的吗?它真的需要创建嵌套OL结构吗?
#3
1
function testInternal($str){
if(is_array($str))
return implode('',array_map('testInternal', $str));
else
return '<li>'.$str.'</li>';
}
function test($str){
echo '<ol>'.testInternal($str).'</ol>';
}