PHP $ array [] = $ content无法正常工作 [英] PHP $array[]=$content not working as it ought

查看:58
本文介绍了PHP $ array [] = $ content无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我已经在这个头发上梳了好一小时或更长时间了.我无法弄清楚为什么它不起作用,特别是因为它与我在起作用起作用的其他地方使用的代码完全相同.

Okay, I have been tearing my hair (and beard) out at this one for a good hour or more now. I can't work out why it isn't working, particularly as it's exactly the same code I use elsewhere which does work.

$sql = "SELECT * FROM `aio_log` ORDER BY `log_timestamp` DESC LIMIT 10;"; // A 'WHERE' will be put in here
$logs = array();
$one_log = array();

if ($stmt = $mysqli->prepare($sql)) {
  $stmt->execute();
  $stmt->store_result();
  $stmt->bind_result($one_log['id'], $one_log['timestamp'],     $one_log['headline'],
                     $one_log['text'], $one_log['link'], $one_log['font_awesome'],
                     $one_log['type']);

  while ($stmt->fetch()) {
    if ($one_log['link'] == "") $one_log['link'] = '#';
    // print_r($one_log); shows the expected output of the database row.
    $logs[] = $one_log;
    //array_push($logs, $one_log); // gives the same - incorrect - response as the above line
  }
}
print_r($logs); // Shows the right number of returned rows from the database, but all showing
// an identical content of the last database row returned.

此代码与我在其他地方使用的代码之间的唯一区别可能是,该代码没有任何SQL语句的绑定部分,因为它是一个简单的无论如何都选择"语句,而作为位工作正常,我看不到这是问题所在.问题显然在$logs[] = $one_log位的某个地方,但是由于我在其他地方使用过它,并且使用array_push得到的结果完全相同,所以我的智慧到此为止!

Probably the only difference between this code and code I've used elsewhere is that this one doesn't have any binded parts of the SQL statement as it's a simple 'select everything no matter what' statement, but as the print_r($one_log) bit works fine, I cannot see this being the issue. The issue is obviously somewhere in the $logs[] = $one_log bit, but as I've used it elsewhere, and get exactly the same result using array_push, I'm at my wits end here!

有想法的人吗?

推荐答案

这很奇怪.作业

$logs[] = $one_log;

推入到数组时,应该复制$one_log.但是显然,这与bind_param将引用绑定到变量这一事实并没有达到预期的交互作用-当您获取新行时,这些引用显然是在引用副本以及原始副本.这可能是PHP使用写时复制方式的怪异结果.更新参考似乎不被认为是触发复制的写操作.

is supposed to make a copy of $one_log when it pushes it onto the array. But apparently this isn't interacting as expected with the fact that bind_param binds references to the variables -- when you fetch the new row, the references are apparently referring to the copy as well as the original. This is probably a weird consequence of the way that PHP uses copy-on-write; updating the reference seems not to be considered a write that triggers the copy.

我建议您使用普通变量而不是$one_log数组.

I suggest you use normal variables instead of the $one_log array.

$stmt->bind_result($id, $timestamp, $headline, $text, $link, $font_awesome, $type);

,然后在访存循环中使用:

and then in the fetch loop use:

$logs[] = array('id' => $id, 'timestamp' => $timestamp, 'headline' => $headline, 
                'text' => $text, 'link' => $link, 'font_awesome' => $font_awesome, 
                'type' => $type);

这篇关于PHP $ array [] = $ content无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆