Home > Backend Development > PHP Tutorial > 有两个数组,长度相同,去重后,如何还能保持一致。

有两个数组,长度相同,去重后,如何还能保持一致。

WBOY
Release: 2016-06-23 14:25:16
Original
979 people have browsed it

比如
$t1=Array ( [0] => 南昌 [1] => 南昌 [2] => 赣州 [3] => 九江 [4] =>赣州  [5] => 九江) 
$t2=Array ( [0] => 2013-09-23 17:48:33  [1] => 2013-09-23 12:48:42  [2] => 2013-09-21 17:48:23  [3] => 2013-09-12 08:40:03  [4] => 2013-09-23 10:28:22  [5] => 2013-09-27 11:28:13 )

现在$t1和$t2 都是相同长度。而且他们还存在一个对应关系。那就是t1[0]和t2[0]。。t1[1]和t2[1].....t1[i]和t2[i]是相关联的,

我现在想实现不重复t1里面的value不重复。然后在和t2对应,如果有重复的,就保留t2里面时间最小的那个。
并且新的数组t22里面的的value是从小到大的顺序。
得到的结果我想是,
$t11=Array ( [0] => 九江  [1] => 赣州 [2] => 南昌 ) 
$t22=Array ( [0] => 2013-09-12 08:40:03 [1] => 2013-09-21 17:48:23  [2] => 2013-09-23 12:48:42 )

或者把t1,t2合并成一个二维数组更方便实现也可以,感谢。


回复讨论(解决方案)

$t1 = Array ( 0 => '南昌', 1 => '南昌', 2 => '赣州', 3 => '九江', 4 => '赣州', 5 => '九江'); $t2 = Array ( 0 => '2013-09-23 17:48:33', 1 => '2013-09-23 12:48:42', 2 => '2013-09-21 17:48:23', 3 => '2013-09-12 08:40:03', 4 => '2013-09-23 10:28:22', 5 => '2013-09-27 11:28:13' );foreach(array_map(null, $t1, $t2) as $r) {  if(! isset($t[$r[0]])) $t[$r[0]] = $r[1];  else $t[$r[0]] = min($t[$r[0]], $r[1]);}asort($t);$t11 = array_keys($t);$t22 = array_values($t);print_r($t11);print_r($t22);
Copy after login
Array
(
    [0] => 九江
    [1] => 赣州
    [2] => 南昌
)
Array
(
    [0] => 2013-09-12 08:40:03
    [1] => 2013-09-21 17:48:23
    [2] => 2013-09-23 12:48:42
)

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template