我有一个数组:

阵列(4 = > '苹果',7 = >“橙色”,13 = >“李子”)

我想要得到这个数组的第一个元素。预期结果:串苹果

一个要求:它不能通过引用传递来完成,所以array_shift不是一个好的解决方案。

我该怎么做呢?


当前回答

有两个解决方案。

解决方案1 -使用钥匙。你没有说你不能用它。:)

<?php
    // Get the first element of this array.
    $array = array( 4 => 'apple', 7 => 'orange', 13 => 'plum' );

    // Gets the first element by key
    $result = $array[4];

    // Expected result: string apple
    assert('$result === "apple" /* Expected result: string apple. */');
?>

解决方案2 - array_flip() + key()

<?php
    // Get first element of this array. Expected result: string apple
    $array = array( 4 => 'apple', 7 => 'orange', 13 => 'plum' );

    // Turn values to keys
    $array = array_flip($array);

    // You might thrown a reset in just to make sure
    // that the array pointer is at the first element.
    // Also, reset returns the first element.
    // reset($myArray);

    // Return the first key
    $firstKey = key($array);

    assert('$firstKey === "apple" /* Expected result: string apple. */');
?>

解决方案3 - array_keys()

echo $array[array_keys($array)[0]];

其他回答

Use:

$first = array_slice($array, 0, 1);  
$val= $first[0];

默认情况下,array_slice不保留键,所以我们可以安全地使用0作为索引。

我喜欢“列表”的例子,但是“列表”只适用于赋值的左边。如果我们不想赋值一个变量,我们将被迫创建一个临时名称,这在最好的情况下会污染我们的作用域,在最坏的情况下会覆盖现有的值:

list($x) = some_array();
var_dump($x);

上面的代码将覆盖$x的任何现有值,只要这个作用域是活动的,$x变量就会一直存在(函数/方法的结束,如果我们在顶层,则永远存在)。这可以使用call_user_func和一个匿名函数来解决,但它很笨拙:

var_dump(call_user_func(function($arr) { list($x) = $arr; return $x; },
                        some_array()));

如果我们使用这样的匿名函数,我们实际上可以避开reset和array_shift,即使它们使用引用传递。这是因为调用一个函数会绑定它的参数,这些参数可以通过引用传递:

var_dump(call_user_func(function($arr) { return reset($arr); },
                        array_values(some_array())));

然而,这实际上是多余的,因为call_user_func将在内部执行这个临时赋值。这让我们可以像处理值传递一样处理引用传递函数,没有任何警告或错误:

var_dump(call_user_func('reset', array_values(some_array())));

这有点晚了,但我遇到了一个问题,我的数组包含数组元素作为其子元素,因此我不能获得第一个数组元素的字符串表示形式。通过使用PHP的current()函数,我管理了这个:

<?php
    $original = array(4 => array('one', 'two'), 7 => array('three', 'four'));
    reset($original);  // to reset the internal array pointer...
    $first_element = current($original);  // get the current element...
?>

感谢所有当前的解决方案帮助我得到这个答案,我希望这能帮助到别人!

你可以通过下面的代码获取第一个元素:

$array_key_set = array_keys($array);
$first_element = $array[$array_key_set[0]];

或者使用:

$i=0;
foreach($array as $arr)
{
  if($i==0)
  {
    $first_element=$arr;
    break;
  }
 $i++;
}
echo $first_element;

同样值得记住的是您执行此操作的上下文,因为详尽的检查可能是昂贵的,而且并不总是必要的。

例如,这个解决方案在我使用它的情况下工作得很好(但显然不能在所有情况下都依赖它……)

 /**
 * A quick and dirty way to determine whether the passed in array is associative or not, assuming that either:<br/>
 * <br/>
 * 1) All the keys are strings - i.e. associative<br/>
 * or<br/>
 * 2) All the keys are numeric - i.e. not associative<br/>
 *
 * @param array $objects
 * @return boolean
 */
private function isAssociativeArray(array $objects)
{
    // This isn't true in the general case, but it's a close enough (and quick) approximation for the context in
    // which we're using it.

    reset($objects);
    return count($objects) > 0 && is_string(key($objects));
}