Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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,由于已知一个键,如何在一个数组中定位,并将随后的所有键(包括第一个键)放入一个变量 我的密钥已知:“r” 按键停止:“s” 我的阵列: Result=rv您必须解析完整数组,并检查键是否在所需的边界内: foreach( $theArray as $key => $value ) { if( $key >= 'r' && $key < 's' ) { // will enter here with keys 'r' and 'v'

由于已知一个键,如何在一个数组中定位,并将随后的所有键(包括第一个键)放入一个变量

我的密钥已知:“
r

按键停止:“
s

我的阵列:


Result=rv

您必须解析完整数组,并检查键是否在所需的边界内:

foreach( $theArray as $key => $value )
{
    if( $key >= 'r' && $key < 's' )
    {
        // will enter here with keys 'r' and 'v'

        $theArray[ $key ] = $theNewValue
    }
}

假设你的钥匙在r后面,下面是代码:

$keys = array_keys($array);
$flip_keys = array_flip($keys);
$result = array_slice($keys, $flip_keys['r'], $flip_keys['s'] - $flip_keys['r']);

您的数组无效。“r”索引不能有两次。
$update = false
foreach( $theArray as $key => $value )
{
    if( $key == 'r' )
    {
        $update = true;
    }
    elseif( $key == 's' )
    {
        break;
    }

    if( $update )
    {
        // will enter here with keys 'r' and 'v'

        $theArray[ $key ] = $theNewValue
    }
}
$keys = array_keys($array);
$flip_keys = array_flip($keys);
$result = array_slice($keys, $flip_keys['r'], $flip_keys['s'] - $flip_keys['r']);