PDA

Click to See Complete Forum and Search --> : [RESOLVED] remove a [key] from array


LingoOutsider
Jul 14th, 2009, 09:31 AM
How can I remove a key eg [2] from an array

[2] => A [4] => B [6] => C ??

kows
Jul 14th, 2009, 02:37 PM
use unset()?
unset($array[2]);

LingoOutsider
Jul 15th, 2009, 05:46 AM
Just what I'm after :)

Thanks

LingoOutsider
Jul 24th, 2009, 04:32 AM
I have this code...

// $deleteRow is the key number I'm looking to remove.

<?php foreach($itemslist as $key => $value): if($key!=$deleteRow){ ;?>
<input type="hidden" name="items[<?php echo $newkey; ?>]" value="<?php echo $value; ?>" />
<?php }endforeach; ?>


the code will remove the value and key but I would like to move everything down so the keys still read in order [1][2][3][4][5] etc. Any ideas on how to adapt the code for this?

kows
Jul 24th, 2009, 04:41 AM
if you don't need to be specific with keys, let the browser/server figure out what keys to use. just like in PHP, you can append an array without knowing the last key.

<input type="hidden" name="items[]" value="whatever" />

using 5 of these should produce the keys 0 through 4.

if this solution doesn't work for you, then you might be able to apply some logic like, if $key is above $deleterow, then $key is equal to $key minus 1? but, I don't see too many situations where the above wouldn't work.

LingoOutsider
Jul 27th, 2009, 02:21 AM
Doh, you make it sound so simple...

'if $key is above $deleterow, then $key is equal to $key minus 1'

Thanks :)

penagate
Aug 2nd, 2009, 11:57 PM
let the browser/server figure out what keys to use.

Server... The browser simply sends the key/value pairs without regard to their keys or values. The PHP interpreter creates an array when it receives keys with the empty bracket pair.