0

I am having some problems accessing some array data effectively and need a couple of tips.

I am doing a database call that returns data similar to this:

+------+------------+------------+-----------+
| id   | key        | part_num   |    value  |
+------+------------+------------+-----------+
|    1 | application| p1         |          1|
|    2 | application| p1         |          2|
|    3 | capsule    | p1         |          3|
|    3 | capsule    | p1         |          4|
|    4 | instrument | p1         |          5|
|    5 | instrument | p1         |          6|
|    5 | instrument | p1         |          7|
|    1 | application| n8         |          1|
|    2 | application| n8         |          2|
|    3 | capsule    | n8         |          3|
|    3 | capsule    | n8         |          4|
|    4 | instrument | n8         |          5|
+------+------------+------------+-----------+

Note the part_num

I really only need the part_num and value out of this (which i am getting), but i would like to group it in PHP by the part number.

For example something even as simple as printing this:

Part: (p1)
Values: 1, 2, 3, 4, 5, 6, 7.
Part: (n)
Values: 1, 2, 3, 4, 5.

PLEASE NOTE: I can't do this with mysql as I am using this data for other things and I don't want to be doing multiple database requests.

I hope i've made this clear enough, let me know if more info is needed.

0

2 Answers 2

1

Assuming your current data is in the variable $data you can loop through the rows and create a new array $partnums.

$partnums = array();

foreach($data as $row) {
    if (!isset($partnums[$row['part_num']]))
        $partnums[$row['part_num']] = array();
    $partnums[$row['part_num']][] = $row['value'];
}

Now $partnums is an array that has the part_num's for keys... each of which references an array of values associated with that part_num.

To print out the values like you have suggested:

foreach($partnums as $partnum => $values) {
    printf('Part: (%s)'.PHP_EOL, $partnum);
    printf('Values: %s.'.PHP_EOL, implode(', ', $values));
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Jacob, thanks for that. I kind of follow what you're saying but my array isn't quite right yet, it's looking like Array( [p1] => 7 [n8] => 5 ) Sorry to seem naive, i'm not sure how now to iterate through each separate value
Ah!! Did you just edit this? I didn't see that last section :)
I believe you may have looked at RDL's previous solution before he edited it.
0

You would use a loop go through the data. Something like this:

$results = [your query and fetch]

$array = array();
foreach ($results as $rs)
  $array[$rs['part_num']][] = $rs['value'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.