Home >Backend Development >PHP Tutorial >php array将横排变成竖排的实现方法

php array将横排变成竖排的实现方法

WBOY
WBOYOriginal
2016-06-23 13:30:221224browse

东北区 哈尔滨 C 76850.4600 76850.4600 76850.4600
东北区 哈尔滨 C 602267.1390 602267.1390 602267.1390
东北区 哈尔滨 C 55356.1110 55356.1110 55356.1110
东北区 沈阳 B 51855.8950 51855.8950 51855.8950
东北区 沈阳 B 385946.7430 385946.7430 385946.7430
东北区 沈阳 B 1588513.5266 1588513.5266  1588513.5266
相同城市的不同的三个数合并到同一行去,这个php array 咋写啊
变成如下形式:
东北区 哈尔滨 C 76850.4600       602267.1390        55356.1110
东北区 沈阳         B  51855.8950       385946.7430        1588513.5266


有什么思路吗大家


回复讨论(解决方案)

你这个少数啊,一共18组数字啊,最后就6组了?
也就是原始的最后两组数不要了?

for($i=0;$i<count($city_arr);$i++){   $n=0;   $m=0;   for($j=0;$i<count($city_arr);$j++){        $m=$m+1;	if(($city_arr[$i][0]==$city_arr[$j][0]) and ($city_arr[$i][1]==$city_arr[$j][1]) and ($city_arr[$i][2]==$city_arr[$j][2]))	{	  $n=$+1;	  //再记录一下 每次$j的值[少于=3次的,$j值,后边赋值newcity后重新记录]	}	if($m==3 and $n<>3){	   $newcity_arr[]=	array($city_arr[$j][0],$city_arr[$j][1],$city_arr[$j][2]);	   $m = 0;		   $i = $n;	}	if($n == 3 and $n=$m){	   $newcity_arr[]=	array($city_arr[$j][0],$city_arr[$j][1],$city_arr[$j][2]);	   $m = 0;	   $i = $n;	   $n = 0;	}	   }	  }

$ar = array(  array('东北区',	'哈尔滨',	'C',	76850.4600,	76850.4600,	76850.4600),  array('东北区',	'哈尔滨',	'C',	602267.1390,	602267.1390,	602267.1390),  array('东北区',	'哈尔滨',	'C',	55356.1110,	55356.1110,	55356.1110),  array('东北区',	'沈阳',	'B',	51855.8950,	51855.8950,	51855.8950),  array('东北区',	'沈阳',	'B',	385946.7430,	385946.7430,	385946.7430),  array('东北区',	'沈阳',	'B',	1588513.5266,	1588513.5266,	1588513.5266),);$n = 3; //锁定前 3 列$res = array();foreach($ar as $t) {  $k = join('_', array_slice($t, 0, $n));  if(! isset($res[$k])) $res[$k] = array_slice($t, 0, $n);  $res[$k][] = $t[$n];}$res = array_values($res);print_r($res);
Array(    [0] => Array        (            [0] => 东北区            [1] => 哈尔滨            [2] => C            [3] => 76850.46            [4] => 602267.139            [5] => 55356.111        )    [1] => Array        (            [0] => 东北区            [1] => 沈阳            [2] => B            [3] => 51855.895            [4] => 385946.743            [5] => 1588513.5266        ))
怎么输出,就不要我教了吧

Statement:
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