[英]Accessing an associative array by integer index in PHP
I want to set the value of an associative array using the array index of the key/value pair. 我想使用键/值对的数组索引设置关联数组的值。 For example:
例如:
$my_arr = array( "bling" => "some bling", "bling2" => "lots O bling" );
$my_arr[1] = "not so much bling"; // Would change the value with key bling2.
How can this be accomplish this without using the key string? 如何在不使用密钥字符串的情况下实现此目的?
Use array_keys . 使用array_keys 。
$keys = array_keys($my_arr);
$my_arr[$keys[1]] = "not so much bling";
Hope this helps. 希望这可以帮助。
There is no correlation between numeric and associative index keys. 数字和关联索引键之间没有相关性。
When you say you want to set the value of an associative array using the array index of the key/value , then you have to use the given key, setting $array[1]
is not the same as setting $array['foo']
. 当你想要使用键/值的数组索引设置关联数组的值时 ,你必须使用给定的键,设置
$array[1]
与设置$array['foo']
。
Consider this array 考虑这个数组
print_r( array('foo', 'foo' => 'bar', 'baz', 'some' => 'value') );
This will give 这会给
Array
(
[0] => foo
[foo] => bar
[1] => baz
[some] => value
)
The foo is the second element in the array. foo是数组中的第二个元素。 That's the offset , but it has nothing to do with the index 1. As you can see, in that array above, index 1 is associated with
baz
. 这是偏移量 ,但它与索引1无关。如您所见,在上面的数组中,索引1与
baz
相关联。 It is wrong to assume that just because foo
is the first associative key it has anything to do with the actual numeric key 1. Just like some
does not correlate to 2. 假设只是因为
foo
是第一个关联键,它与实际数字键1有任何关系是错误的。就像some
与2不相关。
Likewise, for a mixed array like shown above, the solution with array_keys
suggested elsewhere on this site will not work, because 同样,对于如上所示的混合数组,此站点上其他位置建议的
array_keys
解决方案将无效,因为
print_r( array_keys(array('foo', 'foo' => 'bar', 'baz', 'some' => 'value')) );
will give 会给
Array
(
[0] => 0
[1] => foo
[2] => 1
[3] => some
)
So when you do $array[$keys[1]]
you are really doing $array['foo']
. 所以当你执行
$array[$keys[1]]
你真的在做$array['foo']
。 But if you wanted to access the second associative value in that array ( 'some'
), you cannot do $array[$keys[2]]
because that would evaluate to $array[1]
and that's baz
. 但是如果你想访问该数组中的第二个关联值(
'some'
),你就不能做$array[$keys[2]]
因为那会计算为$array[1]
而那就是baz
。
The Offset of an element is completely unrelated to it's key or value 元素的偏移量与其键或值完全无关
print_r(
array(
100 => 'foo',
'foo' => 'bar',
50 => 'baz',
'some' => 'value'
)
);
really means 真正意思
Array
( //key value offset/position
[100] => foo // 0
[foo] => bar // 1
[50] => baz // 2
[some] => value // 3
)
which means the element at offset 0 is foo although it's key is 100. If you want to extract elements from an array by offset, you have to use 这意味着偏移0处的元素是foo,虽然它的关键是100.如果要通过偏移量从数组中提取元素,则必须使用
$third = array_splice($array, 2, 1);
echo $third[0]; // baz
This would create an array holding only the element at the third position. 这将创建一个仅在第三个位置保持元素的数组。
Or you could use an ArrayIterator
. 或者您可以使用
ArrayIterator
。 The ArrayIterator
implements a Seekable
interface that lets you seek to a specific position/offset in the array and then fetch that: ArrayIterator
实现了一个Seekable
接口,可以让您在数组中寻找特定的位置/偏移量,然后获取:
$iterator = new ArrayIterator($array);
$iterator->seek(3);
echo $iterator->current(); // value
Whilst array_keys()
allows access to the nth key, array_values
will give you the nth value. 虽然
array_keys()
允许访问第n个键,但array_values
将为您提供第n个值。
<?php
$array = [
0 => 'Zero',
'1' => 'One',
'Two' => 'Two',
];
echo array_values($array)[2];
?>
will output 'Two'. 将输出'Two'。
Is there an advantage of one over the other? 一个优于另一个有优势吗? Well, the only minor one I can see is the number of array accesses.
好吧,我能看到的唯一一个次要的是数组访问次数。
With array_keys()
you need to 3. 使用
array_keys()
您需要3。
With array_values()
, you only need 2. 使用
array_values()
,您只需要2。
But, on the other hand, keys are normally smaller and the data could be hugely nested, so, on balance, using the array_keys()
is probably safer. 但是,另一方面,密钥通常较小,数据可能非常嵌套,因此,总而言之,使用
array_keys()
可能更安全。
Try this. 试试这个。 It works for you.
它适合你。
$result= array_values($my_arr); // Array with indexes you need
If the array is large, both array_keys
and array_values
will be wasteful since they will allocate a new array the same size as the original, just to get the nth key (or value). 如果数组很大,则
array_keys
和array_values
都会浪费,因为它们将分配一个与原始数组大小相同的新数组,只是为了得到第n个键(或值)。
array_slice
accepts an integer offset and works on associative arrays. array_slice
接受整数偏移量并且在关联数组上工作。 You can use it to get (and set) the nth key in constant time. 您可以使用它在恒定时间内获取(并设置)第n个键。
// This will at most allocate 2 temporary arrays of 1 element each
$key = array_keys(array_slice($array, $n, 1, true))[0];
$array[$key] = $value;
Another possibility is to convert it to a normal array: 另一种可能性是将其转换为普通数组:
$arraybuff = implode("~~~",$my_arr);
$ arraybuff = implode(“~~~”,$ my_arr);
$my_arr = explode("~~~",$arraybuff);$ my_arr = explode(“~~~”,$ arraybuff);
Where "~~~" is a delimiter that wont occur in your data. 其中“~~~”是您的数据中不会出现的分隔符。
Now you can access the array using numerical indexes equal to the offsets. 现在,您可以使用等于偏移的数字索引来访问数组。
If you still need to retain your associative array, just assign it to a different variable. 如果仍需要保留关联数组,只需将其分配给另一个变量即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.