Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP获取已知当前数组键的前一个数组元素_Php_Arrays - Fatal编程技术网

PHP获取已知当前数组键的前一个数组元素

PHP获取已知当前数组键的前一个数组元素,php,arrays,Php,Arrays,我有一个带有特定键的数组: array( 420 => array(...), 430 => array(...), 555 => array(...) ) 在我的应用程序中,我知道当前密钥(例如555)。我想得到上一个数组元素。在本例中,它是键为430的数组元素。如何在PHP中实现这一点?我尝试使用prev(),但是对于这个函数,我们应该知道当前的数组元素。我没有找到设置当前数组元素的函数。您可以反向迭代数组,并在找到搜索值后返回下一次迭代 $f

我有一个带有特定键的数组:

array(
    420 => array(...), 
    430 => array(...), 
    555 => array(...)
)

在我的应用程序中,我知道当前密钥(例如
555
)。我想得到上一个数组元素。在本例中,它是键为
430
的数组元素。如何在PHP中实现这一点?我尝试使用
prev()
,但是对于这个函数,我们应该知道当前的数组元素。我没有找到设置当前数组元素的函数。

您可以反向迭代数组,并在找到搜索值后返回下一次迭代

$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);
由于键数组是索引,您可以按如下方式移动上一个键:

$keys = array_keys($my_array);
$required_key = (array_search(550,$keys,true) - 1);
这将调整
550的值,并返回其在键内的索引,删除一个以获取上一个索引

键我们使用上一个键从原始数组中获取值

$value = $my_array[$required_key];
一种选择:

要执行此操作,您必须转发它(使用和,可能在之前执行,以确保从数组的开头开始):

然后您可以使用:

根据以后要对数组执行的操作,您可能希望使用内部指针

如果该键在数组中不存在,则有一个无限循环,但这可以通过以下方法解决:

 while(key($array) !== null && key($array) !== $key)

当然,
prev
将不再为您提供正确的值,但我假设您正在搜索的密钥将在数组中。

快速查找解决方案:(如果您必须多次执行此操作)

($array))
会将数组映射键返回到它们在原始数组中的位置,例如
数组(420=>0430=>1555=>2)

And()返回将位置映射到值的数组,例如
数组(0=>/*值$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];
}
@返回上一个键,如果没有上一个键可用,则返回false

例如:

$myhash = array(
    'foo' => 'foovalue',
    'goo' => 'goovalue',
    'moo' => 'moovalue',
    'zoo' => 'zoovalue'
);

echo "TEST: ". getPrevKey('zoo', $myhash); // prints moo

@卢卡·博里奥尼的解决方案很有帮助。 如果要查找上一个和下一个键,可以使用以下功能:

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;
}
示例:

$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];
}

如果您的数据很大,那么最好避免循环。您可以创建自己的自定义函数,如下所示:

$array = array('first'=>'111', 'second'=>'222', 'third'=>'333');

function previous($key, $array) {
  $keysArray = array_keys($array);
  $keyNumber = array_search($key, $keysArray);
  if ($keyNumber === 0) {
    $keyNumber = count($array);
  }
  return $array[$keysArray[$keyNumber - 1]];
}
var_dump(previous("second", $array));
请注意,如果您提供第一个键,那么它将返回最后一个值,就像循环数组一样。你可以随意处理

通过稍微调整,您还可以将其泛化以返回下一个值

至于为什么
prev
不起作用,是因为它没有用于这个目的。它只是将数组的内部指针设置为后面一个,与
next


我希望它有帮助

您可以简单地迭代它。键是否按排序顺序排列?正如您从下面的解释中所看到的,在您的案例中没有设置任何指针的单一原因。秋葵还有一个非常有限的用途。不,键没有排序。很酷的非foreach解决方案实际上可以使用内部数组指针,+1事实上我们通常会使用foreach检查数组,但他有一种不同的方法可以很好地工作,而且它也使用内部指针+所以,你刚才很好地解释了为什么不应该使用这些指针。至于
键($array)
-假设我们有一个常规的枚举数组…;)键($array)
不应该是当前($array)
@RobertPitt:这两种方法都可以<如果内部指针指向数组外部,则code>键将返回
null
,而
current
将返回
false
。我认为
key
使用起来更安全,因为值可能是
false
,但对于
array\u keys
方法,key不太可能是
null
+1。紧凑、透明且易于调整。只有在索引是数字和顺序(升序)的情况下才有效。“用法”部分中的注释具有误导性。如果未找到搜索的键,它将返回false,但如果结果位于第一个键且您正在查找前一个键,则将抛出
未定义的偏移量
通知并返回
null
。最后一个键的值与下一个值相同。
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];
}
$array = array('first'=>'111', 'second'=>'222', 'third'=>'333');

function previous($key, $array) {
  $keysArray = array_keys($array);
  $keyNumber = array_search($key, $keysArray);
  if ($keyNumber === 0) {
    $keyNumber = count($array);
  }
  return $array[$keysArray[$keyNumber - 1]];
}
var_dump(previous("second", $array));