PHP two-dimensional array counting and output problems

Source: Internet
Author: User
PHP two-dimensional array counting and output problem data are as follows
Array
(
[Pn] => 007-023
[Pn_from] => Stock
)
Array
(
[Pn] => 007-007
[Pn_from] => Stock
)
Array
(
[Pn] => 007-023
[Pn_from] => BU Warranty
)
The purpose is to calculate the number of repetitions in the pn and merge a new two-dimensional array.
Array
(
[Pn] => 007-023
[Pn_qty] => 2
[Pn_from] => Stock
)
Array
(
[Pn] => 007-007
[Pn_qty] => 1
[Pn_from] => Stock
)

To calculate the number of duplicates through a loop, it is a bit stuck here ,...

$ 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;


Reply to discussion (solution)

SELECT pn, count (*) as pn_qty, pn_from FROM pn_s group by pn

Use group by pn

ERROR: column "pn_s.pn_from" must appear in the group by clause or be used in an aggregate function

Not sure .. Pn_from must be reflected in the group by statement or judgment statement, but I have other fields to determine the required rows ..


This is data ..

First obtain an array (pn1, pn2, pn3 ,...)
Then use the array_count_values () function
Then, process the result into the final array you want...

The data is incorrect .. Submit it again .. Thank you very much for your help.

The data is as follows:
Array
(
[Pn] => 007-023
[Pn_from] => Stock
)
Array
(
[Pn] => 007-007
[Pn_from] => Stock
)
Array
(
[Pn] => 007-023
[Pn_from] => BU Warranty
)
The purpose is to calculate the number of repetitions in the pn and merge a new two-dimensional array.
Array
(
[Pn] => 007-023
[Pn_qty] => 2
[Pn_from] => Stock
)
Array
(
[Pn] => 007-007
[Pn_qty] => 1
[Pn_from] => Stock
)

To calculate the number of duplicates through a loop, it is a bit stuck here ,...

$ 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;




The data is incorrect .. Submit it again .. Thank you very much for your help.


This is data ..


The same was originally true, but the cycle was too bloated ..

Select pn, pn_from, count (pn) from pn_s where job_no = 32 group by pn, pn_from thank you! Some friends in the PG Group helped write an SQL...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.