PHP two-dimensional array count, output problem

Source: Internet
Author: User
Tags repetition
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 goal is to calculate the number of repetitions in the PN, merging 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
)

Want to calculate the repetition by the loop, do a bit of jam 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

Using GROUP by PN

Error:column "Pn_s.pn_from" must appear in the GROUP by clause or being used in an aggregate function

I can't make a mistake. Pn_from must be reflected in the group by or judgment statement, but I have another field to judge the desired line.


Here's the data.

Get an array first (PN1,PN2,PN3,...) The array
Then use the Array_count_values () function
Then the result is processed into your last desired array ...

Incorrect data: Re-submit: Thanks for the help of two-bit

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 goal is to calculate the number of repetitions in the PN, merging 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
)

Want to calculate the repetition by the loop, do a bit of jam 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;




Incorrect data: Re-submit: Thanks for the help of two-bit


Here's the data.


It was the same thought, 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! The PG Group has a friend to help write a SQL ...

  • Related Article

    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.