PHP:如何获取数组';如果为';什么是关联数组?
我有一个关联数组,当var转储时,它如下所示:PHP:如何获取数组';如果为';什么是关联数组?,php,arrays,Php,Arrays,我有一个关联数组,当var转储时,它如下所示: Array ( [tumblr] => Array ( [type] => tumblr [url] => http://tumblr.com/ ) [twitter] => Array ( [type] => twitter [url] => http
Array
(
[tumblr] => Array
(
[type] => tumblr
[url] => http://tumblr.com/
)
[twitter] => Array
(
[type] => twitter
[url] => https://twitter.com/
)
)
正如您所看到的,键是自定义的“tumblr”和“twitter”,而不是数字0和1
有时我需要通过自定义键获取值,有时我需要通过数字键获取值
是否有任何方法可以让$myarray[0]
输出:
(
[type] => tumblr
[url] => http://tumblr.com/
)
您可以通过以下方式运行阵列: 现在,您的阵列看起来像:
array(2) {
[0]=>
array(2) {
["type"]=>
string(6) "tumblr"
["url"]=>
string(18) "http://tumblr.com/"
} ...
这是因为
array\u values()
只会从数组中获取值,并将数组重置/重新排序/重新设置为数字数组。您可以使用以获取带有数字索引的数组副本。数字键和字符串键在PHP中不可互换。你能描述一下你需要什么吗?PHP中的所有数组都是关联的,即使是数字索引的数组。这意味着,如果指定字符串作为索引,则没有关联的数值@nickb有一个很好的方法来处理下面的问题。@Michael甚至不用问,用别人的代码工作,也不想重写整件事。$myarray+=array\u值($myarray)
将创建一个既可以使用命名索引又可以使用数字索引的数组,这与mysql\u fetch\u数组的工作原理大致相同:)
array(2) {
[0]=>
array(2) {
["type"]=>
string(6) "tumblr"
["url"]=>
string(18) "http://tumblr.com/"
} ...