尽管这很有效,但我很好奇是否有人知道更漂亮的方法,因为这种情况似乎经常出现。
<?php
//Initialy, data is nested up in $some_array[0] ...
$some_array = array(array('somevar' => "someValue", "someOtherVar" => "someOtherValue"));
print_r($some_array);
数组([0]=>数组([somevar]=>someValue[someOtherVar]=>someOtherValue)
// Could the following line be achieved a more elegant fashion?
$some_array = $some_array[0];
print_r($some_array);
// Prints the intended result:
数组([somevar]=>someValue[someOtherVar]=>someOtherValue)
有人知道用原生功能或更优雅的方式实现这一点的方法吗?
谢谢!
您要查找的本机函数称为reset
(Demo):
$some_array = reset($some_array);
明确澄清:current
不是必需的。
您可以使用current
(此处解释),它基本上指向数组中的第一个元素并返回它。
为了绝对确定你得到了第一个元素,你应该重置你的数组,就像这样:
reset($arr)
$firstElement = current($arr)