如何一起改变键和数组?

时间:2015-06-26 03:11:51

标签: php arrays key

<?php 
    $test = array("a" => "horse","b" => "fish");
    $k = array_keys($test);
    print_r($k);
    unset($test["a"]);
    print_r($test);
    print_r($k);
?>

我已将array("a" => "horse","b" => "fish")更改为array("b" => "fish") unset function ; $k未更改。

如何使数组和数组中的键一起改变?
要将$k更改为:

Array
(
    [0] => b
)

不会

Array
(
    [0] => a
    [1] => b
)

1 个答案:

答案 0 :(得分:1)

我从您的问题中了解到您可以简单地使用array_intersect作为

$test = array("a" => "horse", "b" => "fish");
$k = array_keys($test);
unset($test["a"]);
$k = array_intersect(array_keys($test), $k);
print_r($k);