如何在PHP中访问数组的第N个元素

时间:2021-04-04 21:33:24

I'm embarrassed to ask this and it's most likely a duplicate, but my google results are coming up short (im searching incorrectly I guess) and such a basic question is infuriating me.

我很尴尬地问这个并且它很可能是重复的,但是我的谷歌搜索结果很短(我猜错了我猜)并且这样一个基本问题激怒了我。

I have an array containing values I don't know.

我有一个包含我不知道的值的数组。

In java, to have a look at the 2nd entry, I would use something like

在java中,看看第二个条目,我会使用类似的东西

var = array[1]

I understand Php arrays are key-value pairs, but how can I simply look at the nth value in an array to see it's key-value pair, and even better, then access just the key / value?

我理解Php数组是键值对,但是我怎样才能简单地查看数组中的第n个值来查看它的键值对,甚至更好,然后只访问键/值?

3 个解决方案

#1


19  

If your keys are numeric, then it works exactly the same:

如果您的密钥是数字,那么它的工作方式完全相同:

$arr = ['one', 'two', 'three']; // equivalent to [0 => 'one', 1 => 'two', 2 => 'three']
echo $arr[1]; // two

If your keys are not numeric or not continuously numeric, it gets a bit trickier:

如果您的密钥不是数字或不连续数字,它会有点棘手:

$arr = ['one', 'foo' => 'bar', 42 => 'baz'];

If you know the key you want:

如果你知道你想要的钥匙:

echo $arr['foo']; // bar

However, if you only know the offset, you could try this:

但是,如果您只知道偏移量,则可以尝试:

$keys = array_keys($arr);
echo $arr[$keys[1]];

Or numerically reindex the array:

或者数字重新索引数组:

$values = array_values($arr);
echo $values[1];

Or slice it:

或切片:

echo current(array_slice($arr, 1, 1));

Most likely you want to be looping through the array anyway though, that's typically what you do with arrays of unknown content. If the content is unknown, then it seems odd that you're interested in one particular offset anyway.

尽管如此,你很可能想要在数组中循环,这通常是你对未知内容数组所做的。如果内容未知,那么您对一个特定偏移感兴趣似乎很奇怪。

foreach ($arr as $key => $value) {
    echo "$key: $value", PHP_EOL;
}

#2


1  

If you want to access the nth element without knowing the index, you can use next() n times to reach the nth element.

如果要在不知道索引的情况下访问第n个元素,可以使用next()n次来到达第n个元素。

for($i = 0; $i<$n; $i++){
    $myVal = next();
}
echo $myVal;

There are other ways to access a specific element, already mentioned by @deceze.

还有其他方法可以访问@deceze已经提到的特定元素。

#3


0  

If you want to access every N-th element:

如果要访问每个第N个元素:

$n = 3;
foreach (array_keys($arr) as $i => $key) {
    if (($i+1) % $n) {
        continue;
    }

    $value = $arr[$key];
}

#1


19  

If your keys are numeric, then it works exactly the same:

如果您的密钥是数字,那么它的工作方式完全相同:

$arr = ['one', 'two', 'three']; // equivalent to [0 => 'one', 1 => 'two', 2 => 'three']
echo $arr[1]; // two

If your keys are not numeric or not continuously numeric, it gets a bit trickier:

如果您的密钥不是数字或不连续数字,它会有点棘手:

$arr = ['one', 'foo' => 'bar', 42 => 'baz'];

If you know the key you want:

如果你知道你想要的钥匙:

echo $arr['foo']; // bar

However, if you only know the offset, you could try this:

但是,如果您只知道偏移量,则可以尝试:

$keys = array_keys($arr);
echo $arr[$keys[1]];

Or numerically reindex the array:

或者数字重新索引数组:

$values = array_values($arr);
echo $values[1];

Or slice it:

或切片:

echo current(array_slice($arr, 1, 1));

Most likely you want to be looping through the array anyway though, that's typically what you do with arrays of unknown content. If the content is unknown, then it seems odd that you're interested in one particular offset anyway.

尽管如此,你很可能想要在数组中循环,这通常是你对未知内容数组所做的。如果内容未知,那么您对一个特定偏移感兴趣似乎很奇怪。

foreach ($arr as $key => $value) {
    echo "$key: $value", PHP_EOL;
}

#2


1  

If you want to access the nth element without knowing the index, you can use next() n times to reach the nth element.

如果要在不知道索引的情况下访问第n个元素,可以使用next()n次来到达第n个元素。

for($i = 0; $i<$n; $i++){
    $myVal = next();
}
echo $myVal;

There are other ways to access a specific element, already mentioned by @deceze.

还有其他方法可以访问@deceze已经提到的特定元素。

#3


0  

If you want to access every N-th element:

如果要访问每个第N个元素:

$n = 3;
foreach (array_keys($arr) as $i => $key) {
    if (($i+1) % $n) {
        continue;
    }

    $value = $arr[$key];
}