I am using a function
function recursiveArrayToList($array = array()){
echo '<ul>';
foreach ($array as $key => $value) {
echo '<li><a href="#">' . $key . '</a>';
if (is_array($value)) {
recursiveArrayToList($value);
}
echo '</li>';
}
echo '</ul>';
}
echo recursiveArrayToList($array);
that creates a ul list.
I want to hide all elements EXCEPT the first <a>
. This should be displayed.
I used this solution to detect the first element:
function recursiveArrayToList($array = array()){
$first = true;
echo '<ul>';
foreach ($array as $key => $value) {
if ( $first )
{
echo "first element, that should be shown";
$first = false;
}
else
{
echo "all other elements that should be hidden";
}
echo '<li><a href="#">' . $key . '</a>';
if (is_array($value)) {
recursiveArrayToList($value);
}
echo '</li>';
}
echo '</ul>';
}
echo recursiveArrayToList($array);
But now all my elements that do have children get the text "first element that should be shown" not only the first element as expected.
I made a mistake in my question, so to be very clear I will show here what I need:
<ul><---- Show only this element
<li>farm
<ul><---- Do not show
<li>animals</li>
<ul><---- Do not show
<li>horses</li>
<ul>
<li>fred</li>
<li>sam</li>
<li>alan</li>
<li>john</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
And here is my script:
<script>
$(window).load(function(){
$('li a').click(function (e) {
e.preventDefault();
var ullist = $(this).parent().children('ul:first');
ullist.slideToggle();
});
});
</script>