Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts

    removing an array entry in the middle of an array

    I tried using a combination of array_shift() and array_splice() to remove a line from an array, and push everything back. What i mean is, of an array with 6 vaules, remove the 4th vaule, and change 5 and 6, to 4 and 5.

    How can I do this?

    Thank you!
    If I'm postin here, I NEED YOUR HELP!!

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I believe i do it with

    unset($arrayname[keyyouwannaremove]);

    not sure what you exactly want. Like your index thing (6 to 5 etc). If it's just a unidimensional array with only the values, then simply removing an element would be enough. If you have a $key->$value type of array, then you'd need soething a bit more complex, but i can't quite see any function for the keys then.
    maye post the array-structure (a print_r() of such an array)
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yeah, I was going to say that I have heard of no function to easily reset your index keys. And as raf said if you have a $key for $value structure for them, I would recommend chaning it so you have named your key values instead: 'id' => 1, 'name' => 'your name' etc.

  • #4
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not sure if just unset-ing the value works (but it might). If it doesn't array_splice can do it with
    PHP Code:
    $my_array array_splice($my_arrayN1); 
    Which should chop out the Nth element (obviously change N to a number).

  • #5
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, got home and that didnt work so here...
    PHP Code:
    function array_cut($arr$n) {
      
    $pre array_slice($arr0$n 1);
      
    $suf array_slice($arr$n);
      foreach(
    $suf as $sv$pre[] = $sv;
      return 
    $pre;

    should work for arrays with numeric indexes at least.

  • #6
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Well I'm pretty sure that he doesn't care about associative arrays.

  • #7
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If he does (@ a later date) he can just use unset like raf said.

  • #8
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    unset dosn't change the index's. But after looking up that code, it was very usfull in stopping the user from refreshing the page, and doubling their order, so thank you for that.

    As far as the splice, i got it to splice, i just coul't get it to keep the array entrys after the splice.

    i ended up finding a way how to do it, although its not the best way im sure.
    PHP Code:
    $i=0;
    while( 
    $i sizeof($OrdersType) ){
        if(
    $number[$i] == "0"){
            
    $temp sizeof($OrdersType);
            
    $stackType = array();
            
    $j=0;
            while( 
    $j $temp ){
                if(
    $j != $i){
                    
    array_push($stackType$OrdersType[$j]);
                }
                
    $j++;
            }
            
    $OrdersType $stackType;
        }
        
    $i++;

    It puts the current entry into a new string unless its the one i dont want.

    Thanks for all your help guys!!
    If I'm postin here, I NEED YOUR HELP!!

  • #9
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well the function I posted earlier does work, so you can use that if you need to.


  •  

    Posting Permissions

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