26

1 question type

$transport = array('foot', 'bike', 'car', 'plane');

can i delete the plane ? is there a way ?

2 question type

 $transport = array('', 'bike', 'car', ''); // delate the last line
 $transport = array('', 'bike', 'car', 'ferrari'); // dont the last line
 $transport = array('ship', 'bike', 'car', 'ferrari'); // dont the last line

is there a easy way to delete the last array " if last array value is empty then delete " if not empty then don't delete ? but not to delete the first array ?

5 Answers 5

49
if(empty($transport[count($transport)-1])) {
    unset($transport[count($transport)-1]);
}
Sign up to request clarification or add additional context in comments.

8 Comments

this one is great, but see roddik comment, if ($transport[count($transport)-1] === '') works well and perfect, thanks .
Glad you found what you needed. Just so you understand, roddik's code will delete the last element from the array if it is an empty string. Mine will delete the last element if it is false, null or any of the other values that the empty() function considers empty. Probably either will work fine for you.
@GTorodov: No, I don't. The request is to delete the last element if it is empty.
@frieder: What "trouble"? This doesn't do anything to the array other than remove the last element if it is empty. array_pop() is a perfectly good method, but this one does not prevent adding anything to the array.
it might be a PHP-bug. I had an array, lets say $array = [[0] => "string1", [1] => "string2"]. Then I removed the last element with unset($array[1]). Now When I add an element using $array[] = "string1" the array will look like this: $array = [[0] => "string1", [2] => "string2"]
|
35

The easiest way: array_pop() which will pop an element of the end of the array.

As for the 2nd question:

if (end($transport) == "") { 
    array_pop($transport); 
}

Should handle the second.

EDIT:

Modified the code to conform to the updated information. This should work with associative or indexed based arrays.

Fixed the array_pop, given Scott's comment. Thanks for catching that!

Fixed the fatal error, I guess empty cannot be used with end like I had it. The above code will no longer catch null / false if that is needed you can assign a variable from the end function and test that like so:

$end_item = end($transport);
if (empty($end_item)) { 
    array_pop($transport); 
}

Sorry for posting incorrect code. The above I tested.

14 Comments

so the first array of the transport is empty then delete ?
@Adam Ramadhan, if ($transport[count($transport)-1] === '')
using array_pop makes more sense, as it will work with both associative hashes and index based arrays.
array_pop returns the last element of the array. Your example code will overwrite the entire array with the element the OP does not want.
Sorry about that. Fixed it, but keep Scott's as the answer imo. He deserves it for catching the obvious issues.
|
18

for # 1,

$transport=array_slice($transport,0,count($transport)-1)

1 Comment

There's no need to get the array length: array_slice($transport, 0, -1)
15

You can simply do that by array_pop() function:

array_pop($transport);

1 Comment

For those who prefer php.net over w3schools.com -- php.net/manual/en/function.array-pop.php
3
$endvalue = & end($transport);
array_pop($endvalue);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.