0

All I faced little annoying issue during my project so I need your help. It is pure php stuff.

I get some result($result and $affiliates) from database. This will look like this.

$result => array(3) {
  [0]=>
  object(stdClass) {
    ["id"]=> string(1) "1"
    ["amount"]=>string(6) "100.00"
    ["affiliate"]=>NULL
  }
  [1]=>
  object(stdClass) {
    ["id"]=>string(1) "2"
    ["amount"]=>string(6) "200.00"
    ["affiliate"]=>NULL
  }
  [2]=>
  object(stdClass) {
    ["id"]=>string(1) "3"
    ["amount"]=>string(6) "300.00"
    ["affiliate"]=>NULL
  }
}

$affiliates = array(3) {
  [0]=>
  object(stdClass) {
    ["id"]=>string(1) "1"
    ["affiliate"]=>string(11) "affiliate-1"
  }
  [1]=>
  object(stdClass) {
    ["id"]=>string(1) "2"
    ["affiliate"]=>string(11) "affiliate-2"
  }
  [2]=>
  object(stdClass) {
    ["id"]=>string(1) "3"
    ["affiliate"]=>string(11) "affiliate-3"
  }
}

then I do something with this result.

here is code

$new_result = array();
foreach($result as $key => $one)
{
    foreach($affiliates as $affiliate)
    {
        $new_data = $one;
        $new_data->affiliate = $affiliate->affiliate;
        array_push($new_result, $new_data);
    }
}

print_r($new_result);   // length is now 9

// expected result

array(9) {
  [0]=>object(stdClass) {
    ["id"]=>string(1) "1"
    ["amount"]=>string(6) "100.00"
    ["affiliate"]=>"affiliate-1"
  }
  [1]=>object(stdClass) {
    ["id"]=>string(1) "1"
    ["amount"]=>string(6) "100.00"
    ["affiliate"]=>"affiliate-2"
  }
  [2]=>object(stdClass) {
    ["id"]=>string(1) "1"
    ["amount"]=>string(6) "100.00"
    ["affiliate"]=>"affiliate-3"
  }
  [3]=>object(stdClass) {
    ["id"]=>string(1) "2"
    ["amount"]=>string(6) "200.00"
    ["affiliate"]=>"affiliate-1"
  }
  [4]=>
  object(stdClass) {
    ["id"]=>string(1) "2"
    ["amount"]=>string(6) "200.00"
    ["affiliate"]=>"affiliate-2"
  }
  [5]=>
  object(stdClass) {
    ["id"]=>string(1) "2"
    ["amount"]=>string(6) "200.00"
    ["affiliate"]=>"affiliate-3"
  }
 [6]=>
  object(stdClass) {
    ["id"]=>string(1) "3"
    ["amount"]=>string(6) "300.00"
    ["affiliate"]=>"affiliate-1"
  }
  [7]=>
  object(stdClass) {
    ["id"]=>string(1) "3"
    ["amount"]=>string(6) "300.00"
    ["affiliate"]=>"affiliate-2"
  }
  [8]=>
  object(stdClass) {
    ["id"]=>string(1) "3"
    ["amount"]=>string(6) "300.00"
    ["affiliate"]=>"affiliate-3"
  }
}   

// but get this

array(9) {
  [0]=>
  object(stdClass) {
    ["id"]=>string(1) "1"
    ["amount"]=>string(6) "100.00"
    ["affiliate"]=>"affiliate-3"
  }
  [1]=>
  object(stdClass) {
    ["id"]=>string(1) "1"
    ["amount"]=>string(6) "100.00"
    ["affiliate"]=>"affiliate-3"
  }
  [2]=>
  object(stdClass) {
    ["id"]=>string(1) "1"
    ["amount"]=>string(6) "100.00"
    ["affiliate"]=>"affiliate-3"
  }
  [3]=>
  object(stdClass) {
    ["id"]=>string(1) "2"
    ["amount"]=>string(6) "200.00"
    ["affiliate"]=>"affiliate-3"
  }
  [4]=>
  object(stdClass) {
    ["id"]=>string(1) "2"
    ["amount"]=>string(6) "200.00"
    ["affiliate"]=>"affiliate-3"
  }
  [5]=>
  object(stdClass) {
    ["id"]=>string(1) "2"
    ["amount"]=>string(6) "200.00"
    ["affiliate"]=>"affiliate-3"
  }
 [6]=>
  object(stdClass) {
    ["id"]=>string(1) "3"
    ["amount"]=>string(6) "300.00"
    ["affiliate"]=>"affiliate-3"
  }
  [7]=>
  object(stdClass) {
    ["id"]=>string(1) "3"
    ["amount"]=>string(6) "300.00"
    ["affiliate"]=>"affiliate-3"
  }
  [8]=>
  object(stdClass) {
    ["id"]=>string(1) "3"
    ["amount"]=>string(6) "300.00"
    ["affiliate"]=>"affiliate-3"
  }
}

'affiliate' properties of all elements in 'new_result' array are updated with last element of 'affiliates' array.

I tried to print first element of 'new_result' array for each recurring turn.

foreach($result as $key => $one)
{
    foreach($affiliates as $affiliate)
    {
        $new_data = $one;
        $new_data->affiliate = $affiliate->affiliate;
        array_push($new_result, $new_data);
        print_r($new_result[0]->affiliate);
    }
}

// expected result, as you know

"affiliate-1"
"affiliate-1"
"affiliate-1"
"affiliate-1"
"affiliate-1"
"affiliate-1" ... 9 times

// but suprisingly get this 
"affiliate-1"
"affiliate-2"
"affiliate-3"
"affiliate-3"
"affiliate-3"
"affiliate-3"
"affiliate-3"  
...

I guess this is related to some reference to object value similar to C++. So I tried to do several alternatives but all are same result.

I never experienced this issue before. If anyone knows what is wrong with this, please teach me,

1 Answer 1

1

The 2 loops are generating 3x3 new occurances.

You need to make sure that you are getting the correct affiliate rather than all the affiliates

$new_result = array();
foreach($result as $key => $one)
{
    foreach($affiliates as $affiliate)
    {
        // make sure yuo are getting the relevant affilicate and not all of them
        if ( $one->id == $affiliate->id) {
            $new_data = $one;
            $new_data->affiliate = $affiliate->affiliate;
            $new_result[] = $new_data;
        }
    }
}

RESULT

Array
(
    [0] => stdClass Object
        (   [id] => 1
            [amount] => 100.00
            [affiliate] => affiliate-1
        )
    [1] => stdClass Object
        (   [id] => 2
            [amount] => 200.00
            [affiliate] => affiliate-2
        )
    [2] => stdClass Object
        (    [id] => 3
            [amount] => 300.00
            [affiliate] => affiliate-3
        )
)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, though, this had been corrected already. The error was here. $new_data = $one; => $new_data = clone $one;
As you can see from this code, the clone is not actually necessary in this situation.
Yeah, I thought like that at first. But anyway, it caused problems and 'clone' solved the problem, either. I don't know why this happens really.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.