Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6996313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:08:24+00:00 2026-05-27T20:08:24+00:00

Much like rtrim() to a string, how do I remove the empty elements of

  • 0

Much like rtrim() to a string, how do I remove the empty elements of an array only after the last non-empty element towards the end of the array while avoiding a for or similar loop and possibly using PHP’s array functions instead?

I’m actually looking for the fastest/most efficient/elegant way, but if this is not possible without a for or similar loop or I’m mistaken about “fast/efficient/elegant” especially with PHP’s array functions then I’d be more than happy to learn/know what’s best. Thanks.

Other assumptions:

  1. There can be a series of single or consecutive empty elements before the last non-empty element in the array.
  2. No need to worry about an empty array, but if this case is covered it’s totally fine.

For example:

Array
(
    [0] => ""
    [1] => "text"
    [2] => "text"
    [3] => ""
    [4] => "text"
    [5] => ""
    [6] => ""
    [7] => "text"
    [8] => ""
    [9] => ""
)

would end up being:

Array
(
    [0] => ""
    [1] => "text"
    [2] => "text"
    [3] => ""
    [4] => "text"
    [5] => ""
    [6] => ""
    [7] => "text"
)

and

Array
(
    [0] => "text"
    [1] => "text"
    [2] => "text"
    [3] => "text"
    [4] => "text"
    [5] => "text"
    [6] => "text"
    [7] => ""
    [8] => ""
    [9] => ""
)

would end up being:

Array
(
    [0] => "text"
    [1] => "text"
    [2] => "text"
    [3] => "text"
    [4] => "text"
    [5] => "text"
    [6] => "text"
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:08:25+00:00Added an answer on May 27, 2026 at 8:08 pm

    It’s like you wrote it in your question: As long as the last value is an empty string, remove it from the array:

    while ("" === end($array))
    {
        array_pop($array);
    }
    

    Edit: I have no clue how serious this is, however for the fun, I’ve come up with this which does not uses any loop in user-code, however, loops are involved for sure inside PHP C functions. Probably this can make a difference, no idea, to enjoy responsively:

    $array = array_slice($array, 0, key(array_reverse(array_diff($array, array("")), 1))+1);
    

    How it works:

    • Get an array with all values that are not in array(""). Keys will be preserved (that’s the important part, the keys in your array are 0-n).
    • Get the key of the last element.
    • Slice the array from start to that position.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Much like How to deserialize JSON file starting with an array in Jackson? I
I have a setup much like this: $('.container a').click(function(event) { event.preventDefault(); $('#contents').remove(); $(this).parent().append(<div id=\contents\></div>);
I have a model pretty much like this: Document Release has an embedded array
I would like to know after much searching how I would match different variations
Much like how the video tag can provide multiple source attributes so an mp4
Much like the Stackoverlow reputation rounding, I'm hoping to do the same thing with
My question is very much like Getting the return value of a PL/SQL function
I currently have interfaces much like the following: interface IService { void Start(); IHandler
I'm building an interface much like the built-in Weather application's flipside view, or the
I'm coding an app that works much like Apple's Weather.app: There's a UIPageControl at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.