PHP的“foreach”构造中的无知或错误?

时间:2009-04-22 02:05:31

标签: php arrays codeigniter dataset foreach

我有从MySQL获得的数据集,如下所示:

Array
(
    [0] => Array
        (
            [views] => 14
            [timestamp] => 06/04
            [views_scaled] => 4.9295774647887
            [unix_time] => 1239022177
        )

    [1] => Array
        (
            [views] => 1
            [timestamp] => 19/04
            [views_scaled] => 0.35211267605634
            [unix_time] => 1240194544
        )

        ...
        ...
        ...

) 1

(经过后期处理,'时间戳'之前确实是时间戳,但无论如何都无关紧要)

数组存储在$results上,在我的代码中间我做了类似的事情:

$results = array_merge($results, $new_days);
$a = $results;
foreach ($results as $row)
{
    $unix_time[] = $row['unix_time'];
}
$b = $results;

问题: $a$b都不同。第一个显示数组应该是,第二个显示相同的count(),但它的第四个元素是最后一个元素的副本。据我所知,我没有通过引用传递任何东西,所以$results并不意味着改变(可能是指针,但不是它的内容)。我在Mac OS X 10.5.2上使用PHP 5.2.4。

显而易见的问题:这是某种意图行为,一个错误或我在这里做错了什么? (请不是布尔答案;)

<小时/> 编辑:感谢大家的兴趣,我不知道我应该发布多少额外的代码,除了从数据库中检索数据和{{1解析时间戳并为缺失的日期构建一个新数组(foreach)。这一切都很好。

这段代码跟在我早期发布的代码之后:

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

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

<小时/> 编辑2:再次感谢,此处为the full snippet以及array_pluck()数组$a$b的输出(也在代码注释中引用)。

6 个答案:

答案 0 :(得分:3)

检查你的代码片段,真的很快(即将离开办公室当天),这可能与你的(第一个)循环中通过引用传递的东西有关。尝试使用normal by value并将所有内容存储到一个新的结果数组中。 (将删除可能发生的任何谜团)。也可以尝试让第二个foreach中的第二个$行换一个不同的名字..打败我 - 不能告诉你真正看到这个。

此行和后续代码块也不会执行

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

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

不会把这个称为答案,但它是一个开始看我猜

答案 1 :(得分: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] !!!)
?>

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

答案 2 :(得分:0)

我无法想象这是怎样的行为。这里一定有别的东西。你能否将行为隔离到一小段足以在此发布的代码?可能如果你做了这个bug就会变得很明显。

答案 3 :(得分: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。

答案 4 :(得分:0)

我认为你的问题是结果集实际上不是一个数组,它是一个像数组一样的mysql结果集对象。

我认为如果你浏览每一行,将它分配给一个新的数组,然后进行合并,它就会正常运行。

答案 5 :(得分:0)

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