MyBB Community Forums

Full Version: remove empty keys from array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi all
another small php query

i store in a field in one of my databases

1,4,2,3,6

then fetch the field and explode it

$fetch = $db->fetch_field($somequery, myfield)
$array = explode(",", $fetch)

array formed

$array[0] = 1
$array[1] = 4
$array[2] = 2
$array[3] = 3
$array[4] = 6

then using a small loop remove one of the values

then implode the array and put it back into the database
SAY 4($array[1]) WAS REMOVED ABOVE

its stored as
1,,2,3,6

note the extra comma and space because now the array becomes

$array[0] = 1
$array[1] =          <==BLANK
$array[2] = 2
$array[3] = 3
$array[4] = 6
how can i change it to

$array[0] = 1
$array[1] = 2
$array[2] = 3
$array[3] = 6
How are you "removing" the element? The proper method is to use unset, ie unset($array[1]); Note that this won't change the keys of existing elements, so it'll end up looking like:
$array[0] = 1
$array[2] = 2
$array[3] = 3
$array[4] = 6
yeah ok

heres a snippet from the code that i use

lets say i already fetched the feild and the array is $array

so

$i = 0;
$imax = $rowmax;  // row max would be the count array output
while ($i <=$imax)
     {
         if($array[$i] =="$variable")  // variable is what i wanna compare the values in the array to 
            { 
                 $array[$i] = "";
                 $i = $lateruse;  // store the array key to be used later
             }
          $i++;
       }


see i want to compare the value i have to all the values in the array (all the values in the array will be unique) then i want to get the key associated to that value as well
also i want to remove the value from the key and move all the values up 1 key
hope thats not too confusing Big Grin