【问题标题】:How to append text to specific php array value如何将文本附加到特定的php数组值
【发布时间】:2012-03-14 09:53:21
【问题描述】:

我需要在 MySQL 查询创建的数组中找到某些键,一旦我有了那个键,我需要在键链接到的值上附加一些文本

我已经弄清楚如何使用 array_key_exists 识别键值...我只需要代码 将文本附加到键的关联值

if(array_key_exists("note", $row_dailyNotes))
{
    // stuck here
    $row_dailyNotes(value) = $row_dailyNotes(value)."text to append"
}

【问题讨论】:

  • 你不能给函数赋值。先修复你的代码。

标签: php arrays append


【解决方案1】:
$row_dailyNotes['note'] .= 'text to append';

【讨论】:

    【解决方案2】:

    您可能正在寻找的是:

    $array[$key] = $array[$key] . "text to append";
    

    这使用数组语法在 PHP“数组”中查找值或设置值。

    例子:

    $array["something"] = $array["something"] . "blah blah";
    

    还有一个使用.=string concatenating operator)的简短形式:

    $array[$key] .= "text to append;
    

    【讨论】:

    • 不是附加到键而不是值吗?
    • @user1222646 - 不,$key .= "sometext" 将附加到密钥。
    • @user1222646:这是附加到值,而不是键。见php.net/Array
    【解决方案3】:

    我猜你应该像这样使用连接运算符:

    $row_dailyNotes[$key] .= "text to append";

    【讨论】:

      猜你喜欢
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多