Search code examples
phparraysfor-loopforeachcycle

Unwanted combination of arrays inside a for cycle


It's 2 days that i'm trying to understand why i get so illogical results. Let me explain with an example. I create invoices based on this simple array.

Array
(
      [0] => Array
          (
              [userid] => 208
              [number_of_services] => 3
              [username1] => sara
              [income1] => 10
              [username2] => mark
              [income2] => 18
              [username3] => joe
              [income3] => 12
          )
      [1] => Array
          (
              [userid] => 4
              [number_of_services] => 1
              [username1] => bruce
              [income1] => 5
          )
      [2] => Array
          (
              [userid] => 303
              [number_of_services] => 1
              [username1] => michael      
              [income1] => 7
          )

)

With this array i want to create 3 invoices for clients with the following ids: 208, 4, 303. I can make it with with a simple foreach.

foreach($myarray AS $key => $value)
{
        // create my invoice
}

Now as you can see client with ID 208 has multiple usernames. I store them as [username1], [username2], [username3] and so on.

[0] => Array
        (
            [userid] => 208
            [number_of_services] => 3
            [username1] => sara
            [income1] => 10
            [username2] => mark
            [income2] => 18
            [username3] => joe
            [income3] => 12
        )

I want to display each username as new line (item) in the invoice so i do the following:

foreach($myarray AS $key => $value)
{

    for($n=1; $n<=$value["number_of_services"]; $n++)
    {

        $invoice["itemdescription$n"] = "Username: ".$value["username$n"]."";
        $invoice["itemamount$n"] = $value["income$n"];
        $invoice["itemtaxed$n"] = 1;

    }

}

Now with this cycle i should have:

  • 1 invoice for client 4 with 1 item (bruce -> 5 euro)
  • 1 invoice for client 303 with 1 item (michael -> 7 euro)
  • 1 invoice for client 208 with 3 item (sara -> 10, mark -> 18, joe -> 12)

But i always get:

  • 1 invoice for client 4 with 3 item (bruce -> 5 euro, mark -> 18, joe -> 12)
  • 1 invoice for client 303 with 3 item (michael -> 7 euro, mark -> 18, joe -> 12)
  • 1 invoice for client 208 with 3 item (sara -> 10, mark -> 18, joe -> 12)

I don't get the reason why the script keeps "merging" [username2], [income2] and [username3], [income3] of client 208 into client 4 and 303! I debugged the script in every single step and the problems seems to be in the for() cycle.

Any idea?


Solution

  • foreach($myarray AS $key => $value)
    {
        for($n=1; $n<=$value["number_of_services"]; $n++)
        {
        $invoice[] = array(
            "itemdescription" => "Username: " . $value . "username" . $n,
            "itemamount" . $n = $value . "income" . $n,
            "itemtaxed" . $n = 1
        );
        }
    }
    

    Try this.