PHP&;数组:计算两个键之间的距离

PHP&;数组:计算两个键之间的距离,php,arrays,pointers,count,position,Php,Arrays,Pointers,Count,Position,具有类似于以下阵列的阵列: $steps = array(0 => 'aaa', 1 => 'bbb', 2 => 'ccc', ......, 7 => 'hhh', 8 => 'iii', .....); 如何计算从键7到键2的顺序需要多少步(键)?如果您的数字键从未丢失任何数字,则可以使用基本减法 如果您需要说明可能丢失的数字,或者键不是数字,您可以使用array\u keys()和array\u search()的组合: 如果您的数字键从不缺少任何数字,则

具有类似于以下阵列的阵列:

$steps = array(0 => 'aaa', 1 => 'bbb', 2 => 'ccc', ......, 7 => 'hhh', 8 => 'iii', .....);

如何计算从键7到键2的顺序需要多少步(键)?

如果您的数字键从未丢失任何数字,则可以使用基本减法

如果您需要说明可能丢失的数字,或者键不是数字,您可以使用
array\u keys()
array\u search()
的组合:


如果您的数字键从不缺少任何数字,则可以使用基本减法

如果您需要说明可能丢失的数字,或者键不是数字,您可以使用
array\u keys()
array\u search()
的组合:


我通过编写以下代码解决了这个问题:

$tot_step=0;
$steps=数组(
0=>“aaa”,
1=>“bbb”,
2=>“ccc”,
3=>“ddd”,
4=>“eee”,
5=>“fff”,
6=>‘ggg’,
7=>“hhh”,
8=>“iii”,
9=>“jjj”,
10=>“aaa”
);
$from=“ddd”;
$to=“bbb”;
$from_index=array_search($from,$steps);
$to_index=array_search($to,$steps);
$last=$steps[(计数($steps)-1)];
如果($from_index>0){
如果($to==$last)
$to_index=(计数($steps)-1);
$arr_l=计数($steps);
$mila=array();
对于($ind=$from_index;$ind=0;$i--)
数组_unshift($steps,$mila[$i]);
}
$to_new=数组搜索($to,$steps);
foreach($key=>$value的步骤){
如果($key==$to_new)
打破
其他的
$tot_step++;
}
}elseif($from_index==0){
如果($to_index==$from_index){
$tot_step=(计数($steps)-1);
}否则{
foreach($key=>$value的步骤){
如果($key==$to_索引)
打破
其他的
$tot_step++;
}
}
}
echo$tot_步骤;

我希望它对某人有用

我通过编写以下代码解决了这个问题:

$tot_step=0;
$steps=数组(
0=>“aaa”,
1=>“bbb”,
2=>“ccc”,
3=>“ddd”,
4=>“eee”,
5=>“fff”,
6=>‘ggg’,
7=>“hhh”,
8=>“iii”,
9=>“jjj”,
10=>“aaa”
);
$from=“ddd”;
$to=“bbb”;
$from_index=array_search($from,$steps);
$to_index=array_search($to,$steps);
$last=$steps[(计数($steps)-1)];
如果($from_index>0){
如果($to==$last)
$to_index=(计数($steps)-1);
$arr_l=计数($steps);
$mila=array();
对于($ind=$from_index;$ind=0;$i--)
数组_unshift($steps,$mila[$i]);
}
$to_new=数组搜索($to,$steps);
foreach($key=>$value的步骤){
如果($key==$to_new)
打破
其他的
$tot_step++;
}
}elseif($from_index==0){
如果($to_index==$from_index){
$tot_step=(计数($steps)-1);
}否则{
foreach($key=>$value的步骤){
如果($key==$to_索引)
打破
其他的
$tot_step++;
}
}
}
echo$tot_步骤;

我希望它对某些人有用

也许只是
7-2
?也许只是
7-2
?谢谢你的回答,但我不必从键2离开,而是从键7离开,沿着数组的正确方向,然后在到达数组末尾后从索引0开始。这是不可能的推理还是扭曲?谢谢你的回答,但我不必从键2离开,而是从键7离开,沿着数组的正确方向,然后在到达数组末尾后从索引0开始。这是不可能的推理,还是只是扭曲?
$array = array(
    0 => 'aaa',
    1 => 'bbb',
    3 => 'ccc',
    'four' => 'ddd',
    900 => 'eee',
    13 => 'fff'
);

$from = 1;
$to = 900;

$keys = array_keys($array);

$from_index = array_search($from, $keys); // 1
$to_index = array_search($to, $keys); // 4

$steps = $to_index - $from_index;
// 3 steps: from 1-3, from 3-'four' and from 'four'-900
$tot_step = 0;

$steps = array(
    0 => 'aaa',
    1 => 'bbb',
    2 => 'ccc',
    3 => 'ddd',
    4 => 'eee',
    5 => 'fff',
    6 => 'ggg',
    7 => 'hhh',
    8 => 'iii',
    9 => 'jjj',
    10 => 'aaa'
);

$from = "ddd";
$to = "bbb";

$from_index = array_search($from, $steps);
$to_index = array_search($to, $steps);

$last = $steps[(count($steps)-1)];

if ($from_index > 0) {

    if ($to == $last)
        $to_index = (count($steps)-1);

    $arr_l = count($steps);
    $mila = array();

    for ($ind = $from_index; $ind <= ($arr_l-1); $ind++) {

        if ($to == $last) {

            if ($steps[$ind] != $last)
                $mila[] = $steps[$ind];

        } else {

            $mila[] = $steps[$ind];

        }

        unset($steps[$ind]);

    }

    if (!empty($mila)) {

        for ($i = (count($mila)-1); $i >= 0; $i--)
            array_unshift($steps, $mila[$i]);

    }

    $to_new = array_search($to, $steps);

    foreach ($steps as $key => $value) {

        if ($key == $to_new)
            break;
        else
            $tot_step++;

    }


} elseif ($from_index == 0) {

    if ($to_index == $from_index) {

        $tot_step = (count($steps)-1);

    } else {

        foreach ($steps as $key => $value) {

            if ($key == $to_index)
                break;
            else
                $tot_step++;

        }

    }

}

echo $tot_step;