I got an array and check if end() exists or is empty. If it is empty I would like to get the previous object of the array. I tried using prev() but that didn’t seem to work when applied to end().
any suggestions? the array can vary in lenght and size.
Thanks for your help guys!
Some code:
$pagination = esc_url( get_next_posts_page_link()); // returns url with slashes
$parts = explode('/', $pagination);
//get the last item
$tag = end($parts);
if ($tag == ""){echo prev($tag); } else{echo $tag;}
You should not apply
prev()on the result ofend()but on the original array as both functions are manipulating the internal array pointer of that array.So:
Edit: By the way, you can also do a
trim($pagination, '/')before youexplodeso that the last item will never be empty.