【问题标题】:The value of element in array keeps changing when push new values with different values推入不同值的新值时,数组中元素的值不断变化
【发布时间】:2022-11-10 01:02:38
【问题描述】:

全部 我在项目期间遇到了一些烦人的问题,所以我需要你的帮助。 它是纯 php 的东西。

我从数据库中得到了一些结果($result 和 $affiliates)。 这看起来像这样。

$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"
  }
}

然后我对这个结果做点什么。

这是代码

$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"
  }
}

'new_result' 数组中所有元素的 'affiliate' 属性都使用 'affiliates' 数组的最后一个元素进行更新。

我尝试为每个循环轮次打印“new_result”数组的第一个元素。

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"  
...

我想这与一些类似于 C++ 的对象值的引用有关。 所以我尝试了几种替代方法,但结果都是一样的。

我以前从未遇到过这个问题。 如果有人知道这是什么问题,请教我,

【问题讨论】:

    标签: php reference associative-array


    【解决方案1】:

    这 2 个循环正在生成 3x3 的新事件。

    您需要确保获得正确的会员而不是所有会员

    $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;
            }
        }
    }
    

    结果

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

    【讨论】:

      猜你喜欢
      • 2014-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-20
      • 1970-01-01
      • 2021-10-30
      • 2022-11-27
      • 1970-01-01
      相关资源
      最近更新 更多