从数组中删除多个元素并重新索引
Remove multiple elements from array and reindex
我可以删除前三个元素和所有 value
不等于 2 的剩余元素吗?语言是 php.
print_r($array)
产出
Array (
[0] => Array ( [name] => aa [value] => 0 )
[1] => Array ( [name] => db [value] => 1 )
[2] => Array ( [name] => bc [value] => 2 )
[3] => Array ( [name] => ba [value] => 3 )
[4] => Array ( [name] => ba [value] => 0 )
[5] => Array ( [name] => bc [value] => 1 )
[6] => Array ( [name] => aa [value] => 2 )
[7] => Array ( [name] => cb [value] => 3 )
[8] => Array ( [name] => ca [value] => 0 )
[9] => Array ( [name] => dc [value] => 1 )
[10] => Array ( [name] => db [value] => 2 )
[11] => Array ( [name] => aa [value] => 3 )
[12] => Array ( [name] => dd [value] => 0 )
[13] => Array ( [name] => db [value] => 1 )
[14] => Array ( [name] => bb [value] => 2 )
[15] => Array ( [name] => ac [value] => 3 )
)
我还需要重新索引以便它从 0 开始
Array (
[0] => Array ( [name] => aa [value] => 2 )
[1] => Array ( [name] => db [value] => 2 )
[2] => Array ( [name] => bb [value] => 2 )
)
你可以这样做:
$new_array = array();
foreach ($array as $k => $v) {
if ($k > 2 && $v['value'] == 2) {
$new_array[] = $v;
}
}
print_r($new_array);
我可以删除前三个元素和所有 value
不等于 2 的剩余元素吗?语言是 php.
print_r($array)
产出
Array (
[0] => Array ( [name] => aa [value] => 0 )
[1] => Array ( [name] => db [value] => 1 )
[2] => Array ( [name] => bc [value] => 2 )
[3] => Array ( [name] => ba [value] => 3 )
[4] => Array ( [name] => ba [value] => 0 )
[5] => Array ( [name] => bc [value] => 1 )
[6] => Array ( [name] => aa [value] => 2 )
[7] => Array ( [name] => cb [value] => 3 )
[8] => Array ( [name] => ca [value] => 0 )
[9] => Array ( [name] => dc [value] => 1 )
[10] => Array ( [name] => db [value] => 2 )
[11] => Array ( [name] => aa [value] => 3 )
[12] => Array ( [name] => dd [value] => 0 )
[13] => Array ( [name] => db [value] => 1 )
[14] => Array ( [name] => bb [value] => 2 )
[15] => Array ( [name] => ac [value] => 3 )
)
我还需要重新索引以便它从 0 开始
Array (
[0] => Array ( [name] => aa [value] => 2 )
[1] => Array ( [name] => db [value] => 2 )
[2] => Array ( [name] => bb [value] => 2 )
)
你可以这样做:
$new_array = array();
foreach ($array as $k => $v) {
if ($k > 2 && $v['value'] == 2) {
$new_array[] = $v;
}
}
print_r($new_array);