How to get last element in position id
How to get last element in position id
How can i get the last element of that position id. it is generating all iterations so i can place a button on that last elements by applying if condition
$quiz = LP_Global::course_item_quiz();
if ( ! $questions = $quiz->get_questions() )
return;
$questions = array_values( $questions );
?>
<ul class="question-numbers">
<?php
foreach ( $questions as $position => $question_id )
$class = $quiz->get_question_number_class( $question_id, ++ $position );
?>
<li class="<?php echo join( ' ', $class ); ?>">
<a href="<?php echo $quiz->get_question_link( $question_id ); ?>">
<span><?php echo $position; ?></span>
</a>
</li>
<?php
?>
</ul>
if ($position == count($questions)-1) {
yes i have applied the condition but it still shows the button
– Talha Hameed
Sep 17 '18 at 22:58
Then I am not sure I understand what yo are trying to do, please try and make your intensions more clear
– RiggsFolly
Sep 17 '18 at 22:59
the class variable is made. can i include function of last position in the class variable and apply if condition after that with the help of class variable ?
– Talha Hameed
Sep 17 '18 at 23:05
<?php foreach ( $questions as $position => $question_id ) if ($position == count($questions)-1) ?> <button type="submit"><?php _e( 'Complete', 'textdomain' ); ?></button> <?php ?> <?php ?>
– Talha Hameed
Sep 17 '18 at 23:14
1 Answer
1
You have new function in PHP7.3+ :
function.array-key-last
foreach($array as $key => $element)
if ($key === array_key_last($array))
echo 'LAST ELEMENT!';
Or you have many ways to get the last element in a foreach loop : Google Search
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
if ($position == count($questions)-1) {
– RiggsFolly
Sep 17 '18 at 22:49