Hello.

I have the following code which pulls an ID, country code, tax rate, state and name from a tbltax table, which uses 'while' to loop through the results. There are currently 3 sets of results, but there could be more or less.

$taxlist  = array();

$taxesquery = "SELECT * FROM tbltax";
$taxesresult = mysql_query($taxesquery);
while ($myrow = mysql_fetch_array($taxesresult, MYSQL_ASSOC)) {
	array_push($taxlist, $myrow["id"], $myrow["level"], $myrow["country"], $myrow["taxrate"], $myrow["state"], $myrow["name"]);
}

I get the following results for the $taxlist array (remember, these are just test values):

Array
(
[0] => 4
[1] => 1
[2] => ZA
[3] => 14.00
[4] =>
[5] => SA VAT
[6] => 3
[7] => 1
[8] => GB
[9] => 15.00
[10] =>
[11] => UK VAT
[12] => 5
[13] => 1
[14] => US
[15] => 20.00
[16] => Washington
[17] => US VAT
)

I can at least get each 'while' pushed to the end of the $taxlist array. However, I would like the $taxlist array to have a better structure so I can easily match against it based on things like the country or state.

If, for example, I want to see if a tax rate exists for US or GB, I need to be able to first see if it exists in the array (the US or GšŸ˜Ž and if it does exist, find the rate for it.

Some tips / advice from a guru would be appreciated.

    Perhaps a multidimensional array that looks like this?

    Array
    (
    [SA]
    (
    [id] => 4
    [level] => 1
    [taxrate] => 14.00
    [state] =>
    [name] => SA VAT
    )
    [GB]
    (
    [id] => 3
    [level] => 1
    [taxrate] => 15.00
    [state] =>
    [name] => UK VAT
    )
    [US]
    (
    [id] => 5
    [level] => 1
    [taxrate] => 20.00
    [state] => Washington
    [name] => US VAT
    )
    )

    I'm just not sure how to change it's structure to be the way i need it so i can match easily against it.

    I may not even need all these values (like id, or name) but i do at least need country, state and rate.

    Any ideas please?

      Okay, i have now managed to get $taxlist like this:

      Array
      (
          [0] => Array
              (
                  [0] => 4
                  [1] => 1
                  [2] => ZA
                  [3] => 14.00
                  [4] => 
                  [5] => SA VAT
              )
      
      [1] => Array
          (
              [0] => 3
              [1] => 1
              [2] => GB
              [3] => 15.00
              [4] => 
              [5] => UK VAT
          )
      
      [2] => Array
          (
              [0] => 5
              [1] => 1
              [2] => US
              [3] => 20.00
              [4] => Washington
              [5] => US VAT
          )
      
      )

      I changed the following line:

      array_push($taxlist, array($myrow["id"], $myrow["level"], $myrow["country"], $myrow["taxrate"], $myrow["state"], $myrow["name"]));
      

      Now i want to try get it looking like this:

      Array
      (
          [ZA] => Array
              (
                  [id] => 4
                  [level] => 1
                  [taxrate] => 14.00
                  [state] => 
                  [name] => SA VAT
              )
      
      [GB] => Array
          (
              [id] => 3
              [level] => 1
              [taxrate] => 15.00
              [state] => 
              [name] => UK VAT
          )
      
      [US] => Array
          (
              [id] => 5
              [level] => 1
              [taxrate] => 20.00
              [state] => Washington
              [name] => US VAT
          )
      
      )

      Anyone able to give me some advice?

        Well, i managed to get it in the structure i was hoping for. I abandoned array_push, because from what i read it was not possible to use associated arrays with it (not sure 100% though)

        Anyway, this is what i eventually got that works:

        $taxlist  = array();
        $taxesquery = "SELECT * FROM tbltax";
        $taxesresult = mysql_query($taxesquery);
        while ($myrow = mysql_fetch_array($taxesresult, MYSQL_ASSOC)) {
            $taxlist[$myrow["country"]] = array();
            $taxlist[$myrow["country"]][id] = $myrow["id"];
            $taxlist[$myrow["country"]][level] = $myrow["level"];
            $taxlist[$myrow["country"]][taxrate] = $myrow["taxrate"];
            $taxlist[$myrow["country"]][state] = $myrow["state"];
            $taxlist[$myrow["country"]][name] = $myrow["name"];
        }
        
        

        I now get an array like this:

        Array
        (
            [ZA] => Array
                (
                    [id] => 4
                    [level] => 1
                    [taxrate] => 14.00
                    [state] => 
                    [name] => SA VAT
                )
        
        [GB] => Array
            (
                [id] => 3
                [level] => 1
                [taxrate] => 15.00
                [state] => 
                [name] => UK VAT
            )
        
        [US] => Array
            (
                [id] => 5
                [level] => 1
                [taxrate] => 20.00
                [state] => Washington
                [name] => US VAT
            )
        
        )
        
        

        Anyone care to comment on this and make any suggestions or tips?

          $taxlist  = array();
          $taxesquery = "SELECT country,id,level,taxrate,state,name FROM tbltax";
          $taxesresult = mysql_query($taxesquery);
          while ($myrow = mysql_fetch_assoc($taxesresult)) {
          	$taxlist[$myrow['country']] = $myrow;
          	unset($taxlist[$myrow['country']]['country'];
          } 
            Write a Reply...