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>
.hidden {
display: none;
}
$class = !$first ? "class='hidden'" : "";
echo '<li ' . $class . '><a href="#">' . $key . '</a>';
$first = false;
Overall this is what you should have:
function recursiveArrayToList($array = array(), $first = true){
echo '<ul>';
foreach ($array as $key => $value) {
$class = !$first ? "class='hidden'" : "";
echo '<li ' . $class . '><a href="#">' . $key . '</a>';
$first = false;
if (is_array($value)) {
recursiveArrayToList($value, false);
}
echo '</li>';
}
echo '</ul>';
}
echo recursiveArrayToList($array);
You can pass a parameter by reference that will indicate if it's the current first element:
function recursiveArrayToList($array = array(), &$first){
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, $first);
}
echo '</li>';
}
echo '</ul>';
}
$first = true;
echo recursiveArrayToList($array, $first);
Note that I passed $first
by reference (hence the &
before the parameter name) - This mean that changes in the value of this parameters inside the function will save to the original $first
that was declared outside the function scope.
Alternative solution, use a default parameter value to the $first
variable:
function recursiveArrayToList($array = array(), $first = true){
echo '<ul>';
foreach ($array as $key => $value) {
if ( $first )
{
echo "first element, that should be shown";
$first = false; // You can comment out this line in order to allow the display of all the first "<li>" on the first level
}
else
{
echo "all other elements that should be hidden";
}
echo '<li><a href="#">' . $key . '</a>';
if (is_array($value)) {
recursiveArrayToList($value, false);
}
echo '</li>';
}
echo '</ul>';
}
echo recursiveArrayToList($array);
Now, when you're calling recursiveArrayToList
on the first time, the $first
is true by default, and the recursive call is passing false
to prevent outputting the string.