我有一个关联数组,当var dumped看起来像这样:
Array
(
[tumblr] => Array
(
[type] => tumblr
[url] => http://tumblr.com/
)
[twitter] => Array
(
[type] => twitter
[url] => https://twitter.com/
)
)
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,键是自定义"tumblr"和"twitter"而不是数字0和1.
有时我需要通过自定义键获取值,有时我需要通过数字键获取值.
有什么方法可以$myarray[0]输出:
(
[type] => tumblr
[url] => http://tumblr.com/
)
Run Code Online (Sandbox Code Playgroud)
您可以通过array_values()以下方式运行阵列:
$myarray = array_values( $myarray);
Run Code Online (Sandbox Code Playgroud)
现在你的数组看起来像:
array(2) {
[0]=>
array(2) {
["type"]=>
string(6) "tumblr"
["url"]=>
string(18) "http://tumblr.com/"
} ...
Run Code Online (Sandbox Code Playgroud)
这是因为array_values()只会从数组中获取值并重置/重新排序/重新键入数组作为数字数组.
| 归档时间: |
|
| 查看次数: |
4071 次 |
| 最近记录: |