在我的项目中,我遇到了一些小麻烦,所以我需要你的帮助。它是纯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//预期结果
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"
}
} //但是拿着这个
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”数组中所有元素的“附属”属性将使用“附属”数组的最后一个元素进行更新。
我试图打印“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);
}
}//预期结果,如你所知
"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++的对象值的引用有关。所以我试着做了几种选择,但结果都是一样的。
我以前从未经历过这个问题。如果有人知道这是怎么回事,请教我,
发布于 2022-11-09 16:56:03
这两个循环正在产生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
)
)https://stackoverflow.com/questions/74378198
复制相似问题