Hi I have an array like this:
array(
'Home' => array(
'About',
'Contact'
),
'News'
);
I wrote this to printing them:
function show($arr){
foreach($arr as $key => $value){
echo "\n<ul>\n<li>\n" . $key;
if( ! empty($value)){
if(is_array($value)){
show($value);
}else{
echo $value;
}
}
echo "\n</li>\n</ul>\n";
}
}
My problem is when I try to echo $value
It'll print something like this:
Home
0About
1Contact
0News
I tried to echo $key
where the echo $value
is here now and I understood it's the key index which is gonna write before the News field or any field that is not an array. I fixed it with turning the single fields to this:
array(
'Home' => array(
'About' => **array()**,
'Contact' => **array()**
),
'News' => **array()**
);
But I don't want to define additional empty arrays!
Peace Out!
function show($arr){
foreach($arr as $key => $value){
echo "\n<ul>\n<li>";
if( ! empty($value)){
if(is_array($value)){
echo '\n'.$key;
show($value);
}else{
echo $value;
}
}
echo "\n</li>\n</ul>\n";
} }
I would say you have to change the place of you echo (of the $key). If it's not an array, you don't care about the key, right ?
function show($arr){
foreach($arr as $key => $value){
if (is_numeric($key))
echo "\n<ul>\n<li>\n";
else
echo "\n<ul>\n<li>\n" . $key;
if( ! empty($value)){
if(is_array($value)){
show($value);
}else{
echo $value;
}
}
echo "\n</li>\n</ul>\n";
}
}
I guess is_numeric should solve your problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.