Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.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_Multidimensional Array - Fatal编程技术网

PHP:查找数组的下一个值和上一个值,这时可能有;“失踪”;索引

PHP:查找数组的下一个值和上一个值,这时可能有;“失踪”;索引,php,multidimensional-array,Php,Multidimensional Array,我正在处理从数据库中检索的一些数据。它被放入一个二维*数组中,并根据数据库中数据的ID手动设置索引 *它不仅仅是二维的,但我只需要处理二维的 这是正在讨论的阵列: Array ( [error] => Boolean(false) [results] => Array ( [1] => Array ( [0] => title1

我正在处理从数据库中检索的一些数据。它被放入一个二维*数组中,并根据数据库中数据的ID手动设置索引

*它不仅仅是二维的,但我只需要处理二维的

这是正在讨论的阵列:

Array
(
    [error] => Boolean(false)
    [results] => Array
        (
            [1] => Array
                (
                    [0] => title1
                    [4] => title2
                )
            [3] => Array
                (
                    [0] => title3
                    [1] => title4
                    [2] => title5
                )
            [4] => Array
                (
                    [0] => title6
                    [1] => title7
                    [3] => title8
                )
        )
)
在这个数组中,我只需要处理其中的“结果”部分,从而使问题数组成为二维的

在我的脚本中发生了一些巫毒魔法,我被告知第一个数组的索引和第二个数组的索引,然后我从第二个数组中提取值

假设我想得到“title5”的值。我的脚本会告诉我需要使用索引3,然后是索引2

我已经完成了所有这些,但这是任务的下一部分,我被困在其中,甚至不知道如何尝试

在检索“title5”之后,我想检索下一个和上一个项目的索引,因此在本例中是“title4”和“title6”的索引。如果没有先前的值(比如我们最初拉取了“title1”),那么我需要某种方法来识别它,比如使用假布尔值。同样,如果也没有“下一个”值

通过检索索引,我的意思是,例如:
title4=3&1
title6=4&0

有人知道我该怎么做吗?我不一定要找人帮我做这项工作,但是如果能给我一些建议,我将不胜感激。与此同时,我会继续尝试。谢谢

注意

  • 标题实际上并不被称为
    title1
    title2
    ,等等。所以我不能只减少一个数字,然后进行搜索

当然,这是一个低级别的解决方案:

$prev = null;
$last_was_value = false;
$next = null;

foreach ( $foo['results'][$index1] as $index => $value ) {
    if($value == $some_result_value) {
        $last_was_value = true; 
    } elseif($last_was_value == true && $next == null) {
        $next = $index;
    } elseif($last_was_value == false) {
        $prev = $index;
    }
}

因此,最后,如果prev或next为null,则您的索引分别是第一个/最后一个索引,如果不是,则这些值是同级索引。

当然,这是一个低级解决方案:

$prev = null;
$last_was_value = false;
$next = null;

foreach ( $foo['results'][$index1] as $index => $value ) {
    if($value == $some_result_value) {
        $last_was_value = true; 
    } elseif($last_was_value == true && $next == null) {
        $next = $index;
    } elseif($last_was_value == false) {
        $prev = $index;
    }
}

因此,最后,如果prev或next为null,那么您的索引分别是第一个/最后一个索引,如果不是,那么这些值就是同级索引。

我稍微编辑了@Jonathan Rich的答案,以获得我的实际解。其中,
$firstIndex
$secondIndex
是我已经知道的两个索引

//my already known indexes
$firstIndex = 3;
$secondIndex = 2;
//
$prevIndex1 = null;
$prevIndex2 = null;
$last_was_value = false;
$nextIndex1 = null;
$nextIndex2 = null;
foreach ( $articles['results'] as $index => $value ) {
    foreach ( $articles['results'][$index] as $index2 => $value2 ) {
        if ( ($index == $firstIndex) && ($index2 == $secondIndex) ) {
            $last_was_value = true;
        } else if ($last_was_value == true) {
            $nextIndex1 = $index;
            $nextIndex2 = $index2;
            break 2;
        } else if ($last_was_value == false) {
            $prevIndex1 = $index;
            $prevIndex2 = $index2;
        }
    }   
}
echo "<br />PREV: $prevIndex1 & $prevIndex2<br />";
echo "NEXT: $nextIndex1 & $nextIndex2<br />";
//我的已知索引
$firstIndex=3;
$secondIndex=2;
//
$prevIndex1=null;
$prevIndex2=null;
$last_was_value=false;
$nextIndex1=null;
$nextIndex2=null;
foreach($articles['results']作为$index=>$value){
foreach($articles['results'][$index]作为$index2=>$value2){
如果($index=$firstIndex)&($index2==$secondIndex)){
$last_was_value=true;
}else if($last_was_value==true){
$nextIndex1=$index;
$nextIndex2=$index2;
破口2;
}else if($last_was_value==false){
$prevIndex1=$index;
$prevIndex2=$index2;
}
}   
}
echo“
PREV:$prevIndex1和$prevIndex2
”; echo“下一步:$nextIndex1和$nextIndex2
”;
我编辑了@Jonathan Rich的答案,以获得我的实际解决方案。其中,
$firstIndex
$secondIndex
是我已经知道的两个索引

//my already known indexes
$firstIndex = 3;
$secondIndex = 2;
//
$prevIndex1 = null;
$prevIndex2 = null;
$last_was_value = false;
$nextIndex1 = null;
$nextIndex2 = null;
foreach ( $articles['results'] as $index => $value ) {
    foreach ( $articles['results'][$index] as $index2 => $value2 ) {
        if ( ($index == $firstIndex) && ($index2 == $secondIndex) ) {
            $last_was_value = true;
        } else if ($last_was_value == true) {
            $nextIndex1 = $index;
            $nextIndex2 = $index2;
            break 2;
        } else if ($last_was_value == false) {
            $prevIndex1 = $index;
            $prevIndex2 = $index2;
        }
    }   
}
echo "<br />PREV: $prevIndex1 & $prevIndex2<br />";
echo "NEXT: $nextIndex1 & $nextIndex2<br />";
//我的已知索引
$firstIndex=3;
$secondIndex=2;
//
$prevIndex1=null;
$prevIndex2=null;
$last_was_value=false;
$nextIndex1=null;
$nextIndex2=null;
foreach($articles['results']作为$index=>$value){
foreach($articles['results'][$index]作为$index2=>$value2){
如果($index=$firstIndex)&($index2==$secondIndex)){
$last_was_value=true;
}else if($last_was_value==true){
$nextIndex1=$index;
$nextIndex2=$index2;
破口2;
}else if($last_was_value==false){
$prevIndex1=$index;
$prevIndex2=$index2;
}
}   
}
echo“
PREV:$prevIndex1和$prevIndex2
”; echo“下一步:$nextIndex1和$nextIndex2
”;
当你说你在寻找下一个和上一个索引时,你的确切意思是什么?你是说如果你检索标题5,位于$results[3][2]。您想知道$results[3][1]和$results[4][0]有一个值吗?我可以给你写一个简短的剧本,我只是想确定我完全明白你在找什么:)没错。如果我收到位于
$results[3][2]
的标题5,那么我希望知道
$results[3][1]
$results[4][0]
中有一个值。我不需要知道这些值是什么,但我需要知道索引。当你说你在寻找下一个和上一个索引时,你到底是什么意思?你是说如果你检索标题5,位于$results[3][2]。您想知道$results[3][1]和$results[4][0]有一个值吗?我可以给你写一个简短的剧本,我只是想确定我完全明白你在找什么:)没错。如果我收到位于
$results[3][2]
的标题5,那么我希望知道
$results[3][1]
$results[4][0]
中有一个值。我不需要知道这些值是什么,但我需要知道索引。感谢您的回复,但是它只在一个维度上工作。我喜欢它背后的想法,但我会玩弄它,试图得到一个有效的解决方案。谢谢你的回答,但它只是在一个单一的维度上工作。我很喜欢它背后的想法,并会玩弄它,试图得到一个有效的解决方案。呸,我在输入另一个解决方案的中途,但这基本上是一样的。在这里,您将面临的唯一问题是,当您实际遍历整个阵列结构时,您的阵列是否过大