简体   繁体   English

使用PHP检索多维数组中的第一个键

[英]Retrieve first key in multi-dimensional array using PHP

I would like to retrieve the first key from this multi-dimensional array. 我想从这个多维数组中检索第一个键。

Array
(
    [User] => Array
        (
            [id] => 2
            [firstname] => first
            [lastname] => last
            [phone] => 123-1456
            [email] => 
            [website] => 
            [group_id] => 1
            [company_id] => 1
        )

)

This array is stored in $this->data. 该数组存储在$ this-> data中。

Right now I am using key($this->data) which retrieves 'User' as it should but this doesn't feel like the correct way to reach the result. 现在我正在使用键($ this-> data)来检索'User',但这不是达到结果的正确方法。

Are there any other ways to retrieve this result? 还有其他方法可以检索此结果吗?

Thanks 谢谢

There are other ways of doing it but nothing as quick and as short as using key() . 还有其他方法可以做到这一点但没有像使用key()那样快速和简短。 Every other usage is for getting all keys. 其他所有用途都是获取所有密钥。 For example, all of these will return the first key in an array: 例如,所有这些都将返回数组中的第一个键:

$keys=array_keys($this->data);
echo $keys[0]; //prints first key

foreach ($this->data as $key => $value)
{
    echo $key;
    break;
}

As you can see both are sloppy. 你可以看到两者都很草率。

If you want a oneliner, but you want to protect yourself from accidentally getting the wrong key if the iterator is not on the first element, try this: 如果你想要一个oneliner,但是如果迭代器不在第一个元素上,你想保护自己不会意外地得到错误的键,试试这个:

reset($this->data);

reset(): 重启():

reset() rewinds array 's internal pointer to the first element and returns the value of the first array element. reset()将数组的内部指针倒回第一个元素,并返回第一个数组元素的值。

But what you're doing looks fine to me. 但是你所做的对我来说很好看。 There is a function that does exactly what you want in one line; 有一个功能可以在一行中完成您想要的功能; what else could you want? 你还想要什么?

使用此(PHP 5.5+):

echo reset(array_column($this->data, 'id'));

I had a similar problem to solve and was pleased to find this post. 我有一个类似的问题要解决,很高兴找到这篇文章。 However, the solutions provided only works for 2 levels and do not work for a multi-dimensional array with any number of levels. 但是,所提供的解决方案仅适用于2个级别,并且不适用于具有任意数量级别的多维阵列。 I needed a solution that could work for an array with any dimension and could find the first keys of each level. 我需要一个可以适用于任何维度的数组的解决方案,并且可以找到每个级别的第一个键。

After a bit of work I found a solution that may be useful to someone else and therefore I included my solution as part of this post. 经过一些工作后,我找到了一个可能对其他人有用的解决方案,因此我将我的解决方案作为本文的一部分。

Here is a sample start array: 这是一个示例启动数组:

    $myArray = array(
    'referrer' => array(
        'week' => array(
            '201901' => array(
                'Internal' => array(
                    'page' => array(
                        'number' => 201,
                        'visits' => 5
                    )
                ),
                'External' => array(
                    'page' => array(
                        'number' => 121,
                        'visits' => 1
                    )
                ),
            ),
            '201902' => array(
                'Social' => array(
                    'page' => array(
                        'number' => 921,
                        'visits' => 100
                    )
                ),
                'External' => array(
                    'page' => array(
                        'number' => 88,
                        'visits' => 4
                    )
                ),
            )
        )
    )
);

As this function needs to display all the fist keys whatever the dimension of the array, this suggested a recursive function and my function looks like this: 因为这个函数需要显示所有的第一个键,无论数组的维度是什么,这建议一个递归函数,我的函数如下所示:

function getFirstKeys($arr){
    $keys = '';
    reset($arr);
    $key = key($arr);
    $arr1 = $arr[$key];
    if (is_array($arr1)){
        $keys .= $key . '|'. getFirstKeys($arr1);
    } else {
        $keys = $key;
    }
    return $keys;
}

When the function is called using the code: 使用代码调用函数时:

$xx = getFirstKeys($myArray);
echo '<h4>Get First Keys</h4>';
echo '<li>The keys are: '.$xx.'</li>';

the output is: 输出是:

Get First Keys 获得第一把钥匙

  • The keys are: referrer|week|201901|Internal|page|number 键是:referrer | week | 201901 |内部|页面|数字

I hope this saves someone a bit of time should they encounter a similar problem. 我希望如果他们遇到类似的问题,这会节省一些时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM