我是php的postgresql新手。我使用 pg_fetch_array()函数在数组下面。
Array
(
[0] =>
[name] =>
[1] => 1
[status] => 1
[2] => C2005
[code] => C2005
)
删除索引1
和键status
的值后,我必须重新索引此数组,以便预期输出应该变为:
Array
(
[0] =>
[name] =>
[1] => C2005
[code] => C2005
)
我试过
unset($row[1]);
unset($row['status'];
$foo = array_values($row);
echo "<pre>";
print_r($foo)
echo "</pre>";
并获得输出
Array
(
[0] =>
[name] =>
[2] => C2005
[code] => C2005
)
从数组中删除特定键后如何重新索引数字索引?
答案 0 :(得分:0)
您可以按键过滤数组。的 Live demo 强>
unset($array['status']);
$number_keys = array_values(array_filter($array, function($k){return is_int($k) && $k != 1;}, ARRAY_FILTER_USE_KEY));
$nonnumber_keys = array_filter($array, function($k){return is_string($k);}, ARRAY_FILTER_USE_KEY);
$result = array_merge($number_keys, $nonnumber_keys);