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>



screenshot






if ($position == count($questions)-1) {

– RiggsFolly
Sep 17 '18 at 22:49


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

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)