【问题标题】:PHP: append string value to arrayPHP:将字符串值附加到数组
【发布时间】:2015-11-12 02:14:00
【问题描述】:

我对 PHP 比较陌生,我不明白为什么我的代码无法将字符串值附加到数组。

从我读过的关于该主题的所有内容中,这段代码:

global $tom_period_parent_title;

$tom_period_parent_title = array();

$tom_period_parent_title[] = 'test';
array_push ($tom_period_parent_title, 'tester');

我应该相信会产生这个结果吗:

tom_period_parent_title
Array
(
    [0] => 'test'
    [1] => 'tester'
)

但由于某种原因,我不明白,也没有在网上找到任何提及,我得到了这个:

tom_period_parent_title
Array
(
    [0] => 0
    [1] => 1
)

谁能帮我理解如何进行这项工作?

【问题讨论】:

  • 向我们展示您的真实代码。
  • 你是怎么得到01的? eval.in/418483
  • 我应该指定的,这是一个全局变量。我正在用Debug This读取全局变量,我认为这是值得信赖的。
  • 什么是Debug This?在你的array_push 之后运行print_r($tom_period_parent_title);
  • 我刚刚复制并运行了您的代码,它产生的正是您想要的......在代码末尾添加print_r( $tom_period_parent_title ) 以查看......

标签: php arrays append


【解决方案1】:

感谢 chris85 向我展示了如何使用 printr() 发现我的代码确实有效。问题是我一直在使用的调试器 Debug This for Wordpress,结果它错误地报告了我的全局数组变量内容。我在其支持页面上报告了该问题。我希望我不会因为这种误解而得到太多的负面评价。谢谢大家。

【讨论】:

    猜你喜欢
    • 2011-01-18
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 2023-01-19
    • 2016-03-02
    • 2020-11-18
    • 2012-07-18
    相关资源
    最近更新 更多