www.webdeveloper.com
Results 1 to 6 of 6

Thread: Array function for reseting index?

  1. #1
    Join Date
    Nov 2003
    Posts
    262

    Array function for reseting index?

    Is there any function for reseting an array index or maybe deletting empty values:

    Lets say my array look like this:

    item[0]="";
    item[1]=1;

    Is it possible to make my array look like?:

    item[0]=1;

    My array is very long, so I need something that does it automatically.
    Something more:
    What happens to an array when it finally contains just a value, is it still an array?

  2. #2
    Join Date
    Nov 2003
    Posts
    262
    Maybe it is possible to delete the empty values of array and them, putting them inside a new one, it should cause them to be in order again.
    For that I need to be able to move values from one array to another.
    Any hints?

  3. #3
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    You could try a for loop, and if the current array index is empty, unset() it.

  4. #4
    Join Date
    Nov 2003
    Posts
    262
    It seems to be easy like that, but it doesn't work:

    <?
    $unidad[0]=cero;
    $unidad[1]=uno;
    $unidad[3]=tres;
    $unidad[4]=cuatro;
    $limite = count($unidad);
    for($i=0;$i<5;$i++){
    if (array_key_exists('$i', $unidad)){$nuevoarray[]=$unidad[$i];}else{unset($unidad[$i]);}
    $nuevoarray[]=$unidad[$i];
    }
    echo "$nuevoarray[1]<br>";
    $limite2 = count($nuevoarray);
    echo "Unidad tiene: ".$limite."<br>";
    echo "Nuevoarray has those values : ".$limite2."<br>";
    echo "this is value 0".$nuevoarray[0]."<br>";
    echo "this is value 1".$nuevoarray[1]."<br>";
    echo "this is value 2".$nuevoarray[2]."<br>";
    echo "this is value 3".$nuevoarray[3]."<br>";
    echo "this is value 4".$nuevoarray[4]."<br>";
    if(isset($unidad[1])){echo "yes, it exist";}else{echo "no, it doesnt";}
    ?>

    In this example $unidad[2] is not set, I'm trying to create a new array in which the index are reset so it should look like:
    $nuevoarray[0]=cero;
    $nuevoarray[1]=uno;
    $nuevoarray[2]=tres;
    $nuevoarray[3]=cuatro;

    But I get nothing, this is the result of the echoing:

    Unidad has those values: 4
    Nuevoarray has those values: 5
    this is value 0
    this is value 1
    this is value 2
    this is value 3
    this is value 4
    no, it doesnt

    ABSOLUTE DESPERATION


  5. #5
    Join Date
    Dec 2002
    Location
    Canada, eh?
    Posts
    228
    Perhaps you're looking for something like this? array_merge(), "If only one array is given and the array is numerically indexed, the keys get reindexed in a continuous way." That should do what I think you're trying to do, basically shifting all values down to fill the next unused numerical key. If that's not what you want, I have no clue what you do want....

  6. #6
    Join Date
    May 2003
    Location
    London, UK
    Posts
    327
    Fwiw, I would personally use array_filter() for this:

    PHP Code:
    <?php

    $array 
    array_filter($array);

    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles