在第二个循环中重用第一个循环中的引用变量会损坏我的数组数据

问题描述 投票:0回答:6

我从 MySQL 获得了一个数据集,如下所示:

数组
(
    [0] => 数组
        (
            [观看次数] => 14
            [时间戳] => 06/04
            [views_scaled] => 4.9295774647887
            [unix_time] => 1239022177
        )

    [1] => 数组
        (
            [观看次数] => 1
            [时间戳] => 19/04
            [views_scaled] => 0.35211267605634
            [unix_time] => 1240194544
        )

        ...
        ...
        ...

)1

(这是经过后期处理的,“时间戳”实际上是之前的时间戳,但这并不重要)

数组存储在

$results
上,在我的代码中间我做了这样的事情:

$results = array_merge($results, $new_days);
$a = $结果;
foreach($结果为$行)
{
    $unix_time[] = $row['unix_time'];
}
$b = $结果;

问题:

$a
$b
都是不同的。第一个按预期显示数组,第二个具有相同的
count()
,但它的第四个元素与最后一个元素重复。据我所知,我没有通过引用传递任何内容,所以
$results
并不意味着要改变(也许是指针,但不是它的内容)。我在 Mac OS X 10.5.2 上使用 PHP 5.2.4。

显而易见的问题:这是否是预期的行为、错误或者我在这里做错了什么? (请不是布尔答案;)


编辑:谢谢大家的兴趣,我不知道到底应该发布多少额外代码,除了从数据库检索数据和解析时间戳和
foreach
之外,我之前没有做太多事情为缺失的日子构建一个新数组 (
$new_days
)。这一切都运行良好。

此代码位于我之前发布的代码之后:

array_multisort($unix_time, SORT_ASC, $结果);
$days = implode('|', array_pluck('timestamp', $results));
$views = implode('|', array_pluck('views', $results));
$views_scaled = implode(',', array_pluck('views_scaled', $results));

array_pluck()
是一个自定义函数,用于从典型的数据库转储数据集中的列生成数组)


编辑2:再次感谢,这是完整的片段以及
$results
数组
$a
$b
(也在代码注释中引用)的输出。

php arrays codeigniter dataset foreach
6个回答
3
投票

非常快地检查您的代码片段(刚刚离开办公室),这可能与您的(第一个)循环中通过引用传递的内容有关。尝试按值使用正常并将所有内容存储到新的结果数组中。 (将消除任何可能发生的谜团)。也可以尝试将第二个 foreach 中的第二个 $row 命名为不同的名称.. 打败了我 - 无法告诉你真正看到这个更多。

这行和下面的代码块也不会执行

if ($last_day != $day_before_this_one AND $last_day)

可能与此有关,新的日子永远不会填满,合并可能会做一些时髦的事情。

不会将此称为答案,但我想这是一个开始


2
投票

问题是第一个 foreach 循环,正如已经提到的。

原因如下...

<?
// set up an example array and loop through it using references (&)
$my_array = array(1,2,3,4);
foreach($my_array as &$item)
{
  $item = $item+.1;
}
// 1st loop, $item points to: $my_array[0], which is now 1.1
// 2nd, $item -> $my_array[1], which is now 2.1
// 3rd, $item -> $my_array[2], which is now 3.1
// 4th, $item -> $my_array[3], which is now 4.1
// looping done, but $item is still pointing to $my_array[3]

// next foreach loop
foreach($my_array as $item)
{
  var_dump($my_array);
  print $item."<br>";
}
// notice the & in the output of the var_dump, if you actually run this code.
// 1st loop: the value of $my_array[0] is assigned to $item, which is still a pointer/reference to $my_array[3]
// first loop.. array(1.1,2.1,3.1,1.1) // grabbing [0] and putting into [3] 
// next loop... array(1.1,2.1,3.1,2.1) // grabbing [1] and putting into [3]
// next loop... array(1.1,2.1,3.1,3.1) // grabbing [2] and putting into [3] (here's where the magic happens!)
// last loop... array(1.1,2.1,3.1,3.1) // grabbing [3] and putting into [3] (but [3] is the same as [2] !!!)
?>

我希望这是有道理的!基本上倒数第二个值将被重复,因为最后一个值在第二次循环期间被替换。


0
投票

我无法想象这是怎样的行为。这里肯定还有其他事情发生。您能否将行为隔离到一段足够小的代码以发布到此处?如果你这样做,这个错误可能会变得明显。


0
投票

我还想说还有其他事情发生。

我写了这个:

<?php

$a = array('bob','sam','tom','harry');
$b = array();
$c = array();

foreach($a as $item) {
        $c[] = $item;
}
$b = $a;

print_r($a);
print_r($b);

并得到:

php ./test.php
Array
(
    [0] => bob
    [1] => sam
    [2] => tom
    [3] => harry
)
Array
(
    [0] => bob
    [1] => sam
    [2] => tom
    [3] => harry
)

不过,我运行的是 PHP 5.2.8。


0
投票

我认为你的问题是结果集并不是真正的数组,它是一个行为类似于数组的mysql结果集对象。

我认为如果你遍历每一行,将其分配给一个新的数组,然后进行合并,它就会正常工作。


0
投票

除非我弄错了,否则这是不久前的一个 PHP 错误。我不知道细节,但数组和引用已经搞砸了一点。

© www.soinside.com 2019 - 2024. All rights reserved.