tags:

views:

86

answers:

3

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 ?

Thanks

Adam Ramadhan

+8  A: 

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.

Brad F Jacobs
so the first array of the transport is empty then delete ?
Adam Ramadhan
I am not understanding the second question fully, please re-elaborate. Your wording is kinda garbled imo. But hopefully, this will give you enough information to work that out on your own. But to delete a full array, unset works.
Brad F Jacobs
ok edited :) sorry.
Adam Ramadhan
@Adam Ramadhan, `if ($transport[count($transport)-1] === '')`
roddik
Right, the code I gave you should only delete the last item of the array if the first item is empty. I believe the code I wrote above does what you want. Give it a try and see, if not try re-working it and reading up a bit more on how arrays works.
Brad F Jacobs
@rodic, you rock. thats a killer if :) @premiso thanks mate, ive edited my post earlier :) so its clearer. ( please see number 3 with a ship ) but if it is empty then your answer is perfect.
Adam Ramadhan
using array_pop makes more sense, as it will work with both associative hashes and index based arrays.
Rob
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.
Scott Saunders
btw its an , Fatal error: Can't use function return value in write context :|
Adam Ramadhan
Sorry about that. Fixed it, but keep Scott's as the answer imo. He deserves it for catching the obvious issues.
Brad F Jacobs
+1 works great ! . well yes he is, there is a slight performance distance, ( im useing xdebug ) .
Adam Ramadhan
Which is better on performance (just curious)? I would put Scotts given he uses count as appose to end and array_pop. But still good information to know :)
Brad F Jacobs
Scott's solution will have better performance if you assign the count to a variable before hand and use that, rather than call count() twice.
Alex JL
+3  A: 
if(empty($transport[count($transport)-1]) {
    unset($transport[count($transport)-1])
}
Scott Saunders
this one is great, but see roddik comment, `if ($transport[count($transport)-1] === '')` works well and perfect, thanks .
Adam Ramadhan
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.
Scott Saunders
+1  A: 

for # 1,

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