【问题标题】:Removing non-unique PHP array values from unassociated array [duplicate]从非关联数组中删除非唯一的 PHP 数组值 [重复]
【发布时间】:2016-06-11 19:15:14
【问题描述】:

以下代码:

$ids=['one','two','three','two','four'];
echo('<pre>'.print_r($ids,1).'</pre>');
$ids = array_unique($ids);
echo('<pre>'.print_r($ids,1).'</pre>');

产生以下内容:

Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => two
    [4] => four
)

Array
(
    [0] => one
    [1] => two
    [2] => three
    [4] => four
)

如何保留连续的数组索引并生成以下内容:

Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
)

【问题讨论】:

标签: php arrays


【解决方案1】:

再添加一行 array_values($ids)

【讨论】:

  • 该死,我离得太近了!谢谢!
【解决方案2】:

您可以使用array_values

查看手册:https://secure.php.net/manual/en/function.array-values.php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    • 2013-01-11
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多