php 一维数组去重

$input = array("a" => "green", "red", "b" => "green", "blue", "red");
 
//常见做法:
$result = array_unique($input);
print_r($result);
 
Array
(
    [a] => green
    [0] => red
    [1] => blue
)
 
//效率提升:
/*
 * 第一种
 *  思路:键值互换,达到去重目的,但是结果集中键值可能并不是按照数字索引的,可通过array_merge重新生成索引
*/
$result_01 = array_flip($input);
$result_02 = array_flip($result_01);
$result    = array_merge($result_02);
 
/*
 * 第二种
 * 思路:键值互换,通过array_key直接获取键值,比array_merge()更快
*/
$result_01 = array_flip($input);
$result    = array_key($result_01);

 

posted @ 2017-10-18 11:20  侠岚之弋痕夕  阅读(390)  评论(0编辑  收藏  举报
Where is the starting point, we don't have a choice, but the destination where we can pursue!