阵推与关联数组 [英] Array push with associate array

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

问题描述

如果我与一个关联数组像这样的工作:

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'));

然而,这给我留下了:

However, this leaves me with:

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
      )

大部分AP preciated!

Much appreciated!

推荐答案

您使用的是关联数组,所以你刚刚设置的键/值对这样的。

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天全站免登陆