Rumah > pembangunan bahagian belakang > tutorial php > 数组合并排序问题

数组合并排序问题

WBOY
Lepaskan: 2016-06-23 14:12:23
asal
863 orang telah melayarinya

本帖最后由 lazygc520 于 2013-06-07 09:37:32 编辑

<?php$sql = "select DISTINCT b.packageno,a.stock_no from `stock_detail` as a,`kw_manage` as b where a.stock_no = '".$stockno."' and a.cust_no = replace( b.cust_no, ' ', '' )";$result = mysql_query($sql);while($row = mysql_fetch_array($result,MYSQL_ASSOC)){	$sql2 = "select lotno,count(lotno) from `vp_barcode_2d_y` where packageno = '".$row['packageno']."' group by lotno order by lotno asc";	$result2 = mysql_query($sql2);	$var = array();	while($row2 = mysql_fetch_array($result2,MYSQL_NUM))	{		$var[] = array('lotno'=>$row2[0],'count'=>$row2[1]);	}	print_r($var);	}//	echo $a[$k]['5'].'<br />';?>
Salin selepas log masuk


得到的结果:

Array(    [0] => Array        (            [lotno] => 3207            [count] => 15        ))Array(    [0] => Array        (            [lotno] => 3207            [count] => 15        ))Array(    [0] => Array        (            [lotno] => 3206            [count] => 12        )    [1] => Array        (            [lotno] => 3207            [count] => 3        ))Array(    [0] => Array        (            [lotno] => 3207            [count] => 15        ))Array(    [0] => Array        (            [lotno] => 3207            [count] => 15        ))Array(    [0] => Array        (            [lotno] => 3218            [count] => 15        ))Array(    [0] => Array        (            [lotno] => 3218            [count] => 15        ))Array(    [0] => Array        (            [lotno] => 3218            [count] => 15        ))Array(    [0] => Array        (            [lotno] => 3218            [count] => 15        ))Array(    [0] => Array        (            [lotno] => 3218            [count] => 15        ))Array(    [0] => Array        (            [lotno] => 3218            [count] => 15        ))Array(    [0] => Array        (            [lotno] => 3218            [count] => 15        ))Array(    [0] => Array        (            [lotno] => 3218            [count] => 15        ))Array(    [0] => Array        (            [lotno] => 3218            [count] => 15        ))Array(    [0] => Array        (            [lotno] => 3218            [count] => 15        ))Array(    [0] => Array        (            [lotno] => 3218            [count] => 15        ))Array(    [0] => Array        (            [lotno] => 3218            [count] => 15        ))
Salin selepas log masuk

如何将这些二维数组合并到一个数组中并按lotno排序?
若lotno相同时,将对应count的值相加。请问下如何处理?


回复讨论(解决方案)

$sql = "select DISTINCT b.packageno,a.stock_no from `stock_detail` as a,`kw_manage` as b where a.stock_no = '".$stockno."' and a.cust_no = replace( b.cust_no, ' ', '' )";$result = mysql_query($sql);$res = array(); //设置一个用于存放结果的数组while($row = mysql_fetch_array($result,MYSQL_ASSOC)){    $sql2 = "select lotno,count(lotno) from `vp_barcode_2d_y` where packageno = '".$row['packageno']."' group by lotno order by lotno asc";    $result2 = mysql_query($sql2);    $var = array();    while($row2 = mysql_fetch_array($result2,MYSQL_NUM))    {       if(isset($res[$row2[0]])) $res[$row2[0]]['count'] += $row2[1];       else $res[$row2[0]] = array('lotno'=>$row2[0],'count'=>$row2[1]);    }}ksort($res); //因键名已处理成 lotno 的值,所以按键名排序就是按 lotno 排序
Salin selepas log masuk
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan