PHP 二维数组计数、输出问题
Array
(
[pn] => 007-023
[pn_from] => Stock
)
Array
(
[pn] => 007-007
[pn_from] => Stock
)
Array
(
[pn] => 007-023
[pn_from] => BU Warranty
)
想要的目的是计算pn中重复的次数,合并一个新的二维数组
Array
(
[pn] => 007-023
[pn_qty] => 2
[pn_from] => Stock
)
Array
(
[pn] => 007-007
[pn_qty] => 1
[pn_from] => Stock
)
想通过循环来计算重复数,做到这里有点卡壳了,。。。
$sql_m = "SELECT pn, pn_from FROM pn_s WHERE job_no = 32";
$result_m = pg_query($sql_m);
$i = 0;
while($row_m = pg_fetch_array($result_m)){
print_r ($row_m);
$i++;
}
echo "
";
echo $i;
回复讨论(解决方案)
SELECT pn,count(*) as pn_qty, pn_from FROM pn_s group by pn
使用group by pn
ERROR: column "pn_s.pn_from" must appear in the GROUP BY clause or be used in an aggregate function
搞不定。。pn_from必须在GROUP BY或者判断语句里面体现出来,但是我又有别的字段来判断所需要的行。。
这个是数据。。
先获取一个array(pn1,pn2,pn3,...)的数组
然后使用array_count_values()函数
然后根据结果处理成你最后想要的数组。。。
数据有误。。重新提交一下。。万分感谢两位的帮助
数据如下
Array
(
[pn] => 007-023
[pn_from] => Stock
)
Array
(
[pn] => 007-007
[pn_from] => Stock
)
Array
(
[pn] => 007-023
[pn_from] => BU Warranty
)
想要的目的是计算pn中重复的次数,合并一个新的二维数组
Array
(
[pn] => 007-023
[pn_qty] => 2
[pn_from] => Stock
)
Array
(
[pn] => 007-007
[pn_qty] => 1
[pn_from] => Stock
)
想通过循环来计算重复数,做到这里有点卡壳了,。。。
$sql_m = "SELECT pn, pn_from FROM pn_s WHERE job_no = 32";
$result_m = pg_query($sql_m);
$i = 0;
while($row_m = pg_fetch_array($result_m)){
print_r ($row_m);
$i++;
}
echo "
";
echo $i;
数据有误。。重新提交一下。。万分感谢两位的帮助
这个是数据。。
原来也是这么想的,但是循环太臃肿了。。
select pn,pn_from,count(pn) from pn_s where job_no=32 group by pn,pn_from 感谢大家!PG群里有朋友帮忙写了个sql。。。