【问题标题】:php array_shift vs unset in associative arrayphp array_shift 与关联数组中的未设置
【发布时间】:2016-12-08 21:58:53
【问题描述】:

你好,关联数组中array_shift和unset有什么区别?

我了解在普通数组中,如果使用 array_shift,键仍然存在,只有第一个值消失,但在关联数组中,我看不出 array_shift 和 unset 之间有什么区别。

下面是代码示例:

    $a=array("a"=>"red","b"=>"green","c"=>"blue");
    $key=key($a);
    unset($a[$key]);
    print_r ($a);

    //result: Array ( [b] => green [c] => blue )

    $b=array("a"=>"red","b"=>"green","c"=>"blue");
    array_shift($b);
    print_r ($b);

    //result: Array ( [b] => green [c] => blue )

【问题讨论】:

标签: php arrays associative-array unset


【解决方案1】:

array_shift 将返回数组的头部(在本例中为条目“a”)

虽然unset 将通过其键删除元素,但假设unset($a['b']) 将留下Array ( [a] => red [c] => blue )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-12
    • 2019-06-24
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 2011-01-04
    • 1970-01-01
    相关资源
    最近更新 更多