V K
V K

Reputation: 31

The value of element in array keeps changing when push new values with different values

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,

Upvotes: 0

Views: 37

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94672

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
        )
)

Upvotes: 1

Related Questions