Results 1 to 7 of 7

Thread: [RESOLVED] remove a [key] from array

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Resolved [RESOLVED] remove a [key] from array

    How can I remove a key eg [2] from an array

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

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: remove a [key] from array

    use unset()?
    PHP Code:
    unset($array[2]); 

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: remove a [key] from array

    Just what I'm after

    Thanks

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: [RESOLVED] remove a [key] from array

    I have this code...
    PHP 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?

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [RESOLVED] remove a [key] from array

    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.

    Code:
    <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.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: [RESOLVED] remove a [key] from array

    Doh, you make it sound so simple...

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

    Thanks

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] remove a [key] from array

    Quote Originally Posted by kows View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width