Jump to content

help with getting rid of null values in array


Gmunky

Recommended Posts

I have an array :

 

Array ( [0] => 10078 [9] => 10293 [10] => 10088 [11] => 10237 [12] => 10211 [13] => 10282 [14] => 10300 [15] => 10110 [16] => 10077 [19] => 10063 [21] => 10296 [22] => 10280 [23] => 10082 [27] => 10079 [29] => 10080 [30] => 10301 [31] => 10291 [32] => 10290 [33] => 10294 [34] => 10111 )

 

 

i want to be able to keep the same order of the values but i need the index to NOT skip! notice the first array is [0] and then the next one is [9]. how can change this so the array numbers are [0],[1],[2],etc.?

thank you for the help!

well I retrieve a number of records from a database and I save those ID's in an array.

       

        //Run query to retrieve all customers of the division//////////////////

$query ="SELECT CUST FROM HYLIB.ARD WHERE DIVI=".$divi." ORDER BY NAME ASC";

    $rs=odbc_exec($conn,$query);

    if(!$rs)

    {

    $errors["query"] = "ARD order# query failed";

        $_SESSION["errors"]=$errors;

        write_to_log($errors,"stopped");

        header('Location: menu.php');

        exit;

    }

    ////////////////////////////////////////////////////////////////////////

   

    /////Save customer IDs in an array//////////

    $i=0;

    while (odbc_fetch_row($rs ))

    {

    $cust[$i] = odbc_result($rs,"CUST");

    $i++;

    }

 

      $cust=array_unique($cust);

    print_r($cust);

   

This is how I get : Array (

 

    * => 10078 [9] => 10293 [10] => 10088 [11] => 10237 [12] => 10211 [13] => 10282 [14] => 10300 [15] => 10110 [16] => 10077 [19] => 10063 [21] => 10296 [22] => 10280 [23] => 10082 [27] => 10079 [29] => 10080 [30] => 10301 [31] => 10291 [32] => 10290 [33] => 10294 [34] => 10111 )

 

Just a note, people love it when you use the [ code ] tags.

 

As far as I can tell the code looks good. Give this a try.

 

<?php
//Run query to retrieve all customers of the division//////////////////
  $query ="SELECT CUST FROM HYLIB.ARD WHERE DIVI=".$divi." ORDER BY NAME ASC";
      $rs=odbc_exec($conn,$query);
      if(!$rs) {
         $errors["query"] = "ARD order# query failed";
          $_SESSION["errors"]=$errors;
          write_to_log($errors,"stopped");
          header('Location: menu.php');
          exit;
      }
      ////////////////////////////////////////////////////////////////////////


      /////Save customer IDs in an array//////////
      $i=0;
      $cust = array(); // make sure array is clean
      while (odbc_fetch_row($rs ))
      {
         $cust[$i++] = odbc_result($rs,"CUST");   
      }

      $cust=array_unique($cust); // this may be problematic??
      print_r($cust);
?>

 

The only issue I could see is if you used $cust before and it was storing some extra data. Also I am not sure if the array_unique is being problematic or not, I do not think so but yea. Something to look into if the array definition does not work.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.