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']);