使用关联数组推送数组 [英] Array push with associate array

查看:42
本文介绍了使用关联数组推送数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我正在使用这样的关联数组:

If I am working with an associate array like such:

Array ( [Username] => user 
        [Email] => email 
      )

我想在最后添加一个元素,我想这样做:

and I want to add an element to the end, I would think to do:

array_push($array, array('Password' => 'pass'));

然而,这给我留下了:

Array ( [Username] => user 
        [Email] => email
        Array ( [Password] => pass )
      )

如何避免这种情况,所以我最终得到:

How can this be avoided so I end up with:

Array ( [Username] => user 
        [Email] => email
        [Password] => pass
      )

非常感谢!

推荐答案

您正在使用关联数组,因此您只需像这样设置键/值对.

You are using an associative array so you just set the key/value pair like this.

$array["Password"] = pass;

我认为您可能需要查看数组和关联数组之间的区别.例如,如果我用不同的值再次运行相同的命令,它会覆盖旧的:

I think you may need to review the difference between an array and an associative array. For example if I ran the same command again with a different value it would overwrite the old one:

$array["Password"] = "overwritten";

给你这个

Array ( [Username] => user 
        [Email] => email
        [Password] => "overwritten"
      )

从你的问题来看,这不是你所期望的

Which judging by your question is not what your expecting

这篇关于使用关联数组推送数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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