我有特定键的数组:PHP获取前数组元素知道目前的数组键
array(
420 => array(...),
430 => array(...),
555 => array(...)
)
在我的应用程序知道当前键(例如555
)。我想获得前面的数组元素。在这个例子中,它是一个密钥为430
的数组元素。我怎样才能在PHP中做到这一点?我试图与prev()
一起工作,但是对于这个函数我们应该知道当前的数组元素。我没有找到函数,什么设置当前数组元素。
我有特定键的数组:PHP获取前数组元素知道目前的数组键
array(
420 => array(...),
430 => array(...),
555 => array(...)
)
在我的应用程序知道当前键(例如555
)。我想获得前面的数组元素。在这个例子中,它是一个密钥为430
的数组元素。我怎样才能在PHP中做到这一点?我试图与prev()
一起工作,但是对于这个函数我们应该知道当前的数组元素。我没有找到函数,什么设置当前数组元素。
一个选项:
为set the internal pointer to a certain position,你必须使用key
和next
,m,并将其转发( aybe做reset
之前,确保你从数组的起点开始):
while(key($array) !== $key) next($array);
然后你可以使用prev()
:
$prev_val = prev($array);
// and to get the key
$prev_key = key($array);
取决于你打算用做什么数组后,你可能想要reset
的内部指针。
如果该键不数组中存在,你有一个无限循环,但是这可能与解决:
while(key($array) !== null && key($array) !== $key)
当然
prev
的
不会给你了正确的价值,但我认为关键无论如何,你正在寻找数组。
酷的非foreach解决方案,实际与内部数组指针,+1 – BoltClock 2011-01-25 11:12:18
您可以反向遍历数组,并在找到搜索值后返回下一个迭代。
$found = false;
foreach(array_reverse($array, true) as $key=>$value) {
if ($found) {
print "$key => $value\n";
break;
} else if ($key == 555) {
$found = true;
}
}
只是遍历数组
$_index = null;
foreach($myarray as $index => $value)
{
if($key == $my_index) // if($key == 550)
{
break;
}
$_index = $index;
}
echo $_index; //the prev key from 550;
另一种解决方案是让你的阵列的键枚举数组中,像这样结束:
$keys = array_keys($my_array);
为键阵列是索引,你可以像上面这样移动前一个键:
$required_key = (array_search(550,$keys,true) - 1);
这将罚款550
值,键内返回它的索引,删除一个获得以前的索引,我们有我们以前的键从原来的阵列中获得的价值
关键
$value = $my_array[$required_key];
用于`array_keys`方法。紧凑,透明,易于调整。 – mmdanziger 2012-06-07 11:48:09
解决方案具有快速查找:(如果你要做一次比这更)
$keys = array_flip(array_keys($array));
$values = array_values($array);
return $values[$keys[555]-1];
array_flip
(
array_keys
($array));
会返回一个数组的键映射其职务原始数组中,例如array(420 => 0, 430 => 1, 555 => 2)
。
而array_values
()将数组映射位置返回到值,例如, array(0 => /* value of $array[420] */, ...)
。
所以$values[$keys[555]-1]
有效地返回以前的元素,鉴于当前具有关键555
替代解决方案:
$keys = array_keys($array);
return $array[$keys[array_search(555, $keys)-1]];
这是拍摄一个和下一个项目一个简单的解决方案,即使我们在数组的末尾。
<?php
$current_key; // the key of the item we want to search from
if (isset($array[$current_key+1]))
{
// get the next item if there is
$array_next = $array[$current_key+1];
}
else
{
// if not take the first (this means this is the end of the array)
$array_next = $array[0];
}
if (isset($array[$current_key-1]))
{
// get the previous item if there is
$array_prev = $array[$current_key-1];
}
else
{
// if not take the last item (this means this is the beginning of the array)
$array_prev = $array[count($array)-1];
}
我解决这样这个问题:
function getPrevKey($key, $hash = array())
{
$keys = array_keys($hash);
$found_index = array_search($key, $keys);
if ($found_index === false || $found_index === 0)
return false;
return $keys[$found_index-1];
}
@return在前关键还是假的,如果没有以前的密钥可
例子:
$myhash = array(
'foo' => 'foovalue',
'goo' => 'goovalue',
'moo' => 'moovalue',
'zoo' => 'zoovalue'
);
echo "TEST: ". getPrevKey('zoo', $myhash); // prints moo
@Luca Borrione的解决方案很有用。 如果你想找到两个前面和后面的按键,你可以使用以下功能:
function getAdjascentKey($key, $hash = array(), $increment) {
$keys = array_keys($hash);
$found_index = array_search($key, $keys);
if ($found_index === false) {
return false;
}
$newindex = $found_index+$increment;
// returns false if no result found
return ($newindex > 0 && $newindex < sizeof($hash)) ? $keys[$newindex] : false;
}
用法:
// previous key
getAdjascentKey($key, $hash, -1);
// next key
getAdjascentKey($key, $hash, +1);
例子:
$myhash = array(
'foo' => 'foovalue',
'goo' => 'goovalue',
'moo' => 'moovalue',
'zoo' => 'zoovalue'
);
getAdjascentKey('goo', $myhash, +1);
// moo
getAdjascentKey('zoo', $myhash, +1);
// false
getAdjascentKey('foo', $myhash, -1);
// false
上的解决方案进一步扩大Luca Borrione和cenk,以便您可以在任一方向上环绕阵列的末端,您可以使用:
function getAdjascentKey($key, $hash = array(), $increment) {
$keys = array_keys($hash);
$found_index = array_search($key, $keys);
if ($found_index === min(array_keys($keys)) && $increment === -1) {
$found_index = max(array_keys($keys))+1;
}
if ($found_index === max(array_keys($keys)) && $increment === +1) {
$found_index = min(array_keys($keys))-1;
}
return $keys[$found_index+$increment];
}
你可以简单地迭代它。 – 2011-01-25 11:04:27
排序顺序中的键? – Gumbo 2011-01-25 11:04:52
正如你可以从下面的解释中看到的,没有任何理由在你的情况下设置任何指针。他们有另一个非常有限的用途。 – 2011-01-25 11:28:05