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:
- There can be a series of single or consecutive empty elements before the last non-empty element in the array.
- 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"
)