【问题标题】:PHP - Append item to array but without altering original (i.e. return new instance)?PHP - 将项目附加到数组但不改变原始(即返回新实例)?
【发布时间】:2014-07-01 20:29:34
【问题描述】:
// default email addresses
$this->defaultEmail = array('no-reply@blah.com' => 'blah');
$consultant = ( isset($_POST['consultant']) ) ? $_POST['consultant'] : "";

$addresses = ( empty($consultant) ) ? $this->defaultEmail : ?? ;

我可以使用array_push($this->defaultEmail, $consultant);

它会改变原来的还是只是$addresses 指针?

【问题讨论】:

  • 你为什么不简单地测试一下呢? (有点建设性,this 可能会有所帮助)
  • 哈哈只是在想……我的提交需要几分钟,所以我想我可能会得到快速响应。
  • 你也可以通过codepad3v4l来测试这些简单的原理

标签: php arrays append


【解决方案1】:

所以也许我没有正确地表达我的问题,但经过一番搞砸之后,这是这样做的方法(如果有人在乎的话):

$addresses = $this->defaultEmail;
if( !empty($consultant) ) {
    array_push($addresses, $consultant);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2021-07-06
    • 2014-11-22
    • 2020-10-30
    • 1970-01-01
    • 2017-05-23
    相关资源
    最近更新 更多