Only first element of response visible - php

Asked
Active3 hr before
Viewed126 times

5 Answers

responsefirstelement
90%

You can add a parameter to recursiveArrayToList called $first = true (example: recursiveArrayToList($array = array(), $first = true)) and when you call it recursively pass false instead of the default true – Alon Eitan Mar 31 '16 at 15:28 ,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., Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,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.

CSS

.hidden {
   display: none;
}

PHP

$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);
load more v
88%

Search Answer Titles

$firstKey = array_key_first($array);
load more v
72%

Despite its name, isset() not only returns false if an item does not exist, but also returns false for null values.,The output of this will be test, as expected.,As explained, isset($postData) will also return false if $postData was set to null. It therefore is possible for isset($postData) to return false even if $_POST['active'] returned true. So again, the above logic is flawed.,Unfortunately, if a class uses the magic __get() function to retrieve a property’s value, there’s no foolproof way to check if that property value is empty or not. Outside of the class’ scope, you can really only check if a null value will be returned, and that doesn’t necessarily mean that the corresponding key is not set, since it actually could have been set to null.

Not sure how to use foreach loops in PHP? Using references in foreach loops can be useful if you want to operate on each element in the array that you are iterating over. For example:

$arr = array(1, 2, 3, 4);
foreach($arr as & $value) {
   $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
load more v
65%

Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2 i.e. when the string representation is the same, the first element will be used. , Note that keys are preserved. If multiple elements compare equal under the given flags, then the key and value of the first equal element will be retained. , SORT_LOCALE_STRING - compare items as strings, based on the current locale. ,Note: Note that array_unique() is not intended to work on multi dimensional arrays.

Array
   (
      [a] => green[0] => red[1] => blue
   )
load more v
75%

value Type: String The value to insert into the input when the item is selected. , item Type: Object label Type: String The string to display for the item. value Type: String The value to insert into the input when the item is selected. ,Bind an event listener to the autocompleteselect event:, label Type: String The string to display for the item.

$(".selector").autocomplete({
   appendTo: "#someElem"
});
load more v

Other "response-first" queries related to "Only first element of response visible - php"