php 取出二维数组中不重复的值?


我有一个类似这样的数组:

   
  Array
  
(
[102] => Array
(
[0] => 36
[1] => 42
[2] => 99
[3] => 186
[4] => 210
[5] => 211
[6] => 213
[7] => 215
[8] => 265
)

[104] => Array
(
[0] => 19
[1] => 42
[2] => 94
[3] => 98
[4] => 106
[5] => 176
[6] => 206
[7] => 210
[8] => 211
[9] => 212
[10] => 213
[11] => 248
[12] => 265
[13] => 271
[14] => 292
)

[105] => Array
(
[0] => 42
[1] => 98
[2] => 206
[3] => 211
[4] => 212
[5] => 213
[6] => 265
)

[106] => Array
(
[0] => 28
[1] => 124
[2] => 127
[3] => 206
[4] => 211
[5] => 212
[6] => 214
)

[111] => Array
(
[0] => 28
[1] => 124
[2] => 127
[3] => 206
[4] => 211
[5] => 212
[6] => 214
)

)

要求:
1、5个大的数组中各取一个值即取出5个值
2、保证取出的5个值各不相同 (5个数组中如果存在相同的数组例如[106] [111] 这两个数组,可以从其中一个取出0(当然可能存在多个个相同的大数组,所以也可以取出多个0) )
最后返回的是键名为 102, 104,105,106,111 ,值为取出非重复值的一维数组

php 数组

麦克塔维斯 11 years, 7 months ago
   
  <?php
  
$arr = array
(
102 => array
(
0 => 36,
1 => 42,
2 => 99,
3 => 186,
4 => 210,
5 => 211,
6 => 213,
7 => 215,
8 => 265,
),

104 => array
(
0 => 19,
1 => 42,
2 => 94,
3 => 98,
4 => 106,
5 => 176,
6 => 206,
7 => 210,
8 => 211,
9 => 212,
10 => 213,
11 => 248,
12 => 265,
13 => 271,
14 => 292,
),

105 => array
(
0 => 42,
1 => 98,
2 => 206,
3 => 211,
4 => 212,
5 => 213,
6 => 265,
),

106 => array
(
0 => 28,
1 => 124,
2 => 127,
3 => 206,
4 => 211,
5 => 212,
6 => 214,
),

111 => array
(
0 => 28,
1 => 124,
2 => 127,
3 => 206,
4 => 211,
5 => 212,
6 => 214,
),

);
// ps: 你给出示例数据时,最好是上面的格式,否则没有人愿意来回答你的问题,
// 因为要把数据格式化成代码可以用的已经很费功夫了,其实我就差点不想回答了

$result = array();
foreach($arr as $k => $v) {
$v = array_diff($v, $result);
$result[$k] = array_shift($v);
}

print_r($result);

_咸鱼汤_ answered 11 years, 7 months ago

Your Answer