1

I figured I would need a multidimensional array of some sort. I just don't know how to get it in a format that is simple to understand. Basically I have a bunch of invoices that go to specific parks. The end result I want to have is a park having multiple invoices showing on the single park so I can just loop it easily. I have multiple sql questions to get the variables but I got it down to this:

            $sql = "SELECT * FROM INVOICE WHERE ID=$ids[$i]";
            $result = $conn->query($sql);

            if ($result->num_rows > 0) {
                $row = $result->fetch_assoc();
                //invoice variables go here
                //row is invoices
                $invoiceapplication = $row['APPLICATION'];
                $invoiceid = $row['ID'];
                $invoicedate = $row['DATE'];
                $late = $row['LATE'];
                $amountowed = '';
                $amountowed = $row['AMOUNTOWED'];
                $originalamountowed = $row['AMOUNTOWED'];


                //grabbing application park id to get all the parks.
                $sqlapplication = "SELECT * FROM APPLICATION WHERE ID=$invoiceapplication";
                $resultapplication = $conn->query($sqlapplication);
                if($resultapplication->num_rows > 0){
                    $rowapplication = $resultapplication->fetch_assoc();
                    //rowapplication is applications
                    //application variables here
                    $applicationparkid = $rowapplication['MOBILEPARK'];
                    //end app variables.
                }
                //finished grabbing application park id to get all the parks.
                echo $invoiceid.' is park '.$applicationparkid.'<br>';

                if(in_array($applicationparkid,$allparkarray)){
                    //if already contained in the allparkarray

                } else {
                    array_push($allparkarray,$applicationparkid);   
                }


            }
            $i++;
        }

At the end of everything this is my output:

901 is park 38
1362 is park 38
1595 is park 38
1825 is park 38
2093 is park 38
4766 is park 38
17018 is park 61
17011 is park 66
17015 is park 66
17014 is park 66
17013 is park 66
17012 is park 66

Ideally... I want to have a multidimensional array like this as output:

38 => (901,1362,1595,1825,2093,4766)
61 => (17018)
66 => (17011,17015,17014,17013,17012)

Keep in mind... It is totally possible for an invoice to be 'out of order' in its output so if a sudden different number appeared in between... I basically cannot rely on a simple 0,1,2,3 system to pair it up.

I am sure there is an easy way to do this I just cannot for the life of me figure it out.

sblumberg
  • 33
  • 6
  • You can use `sql group by` together with some `concat` function. https://stackoverflow.com/questions/149772/how-to-use-group-by-to-concatenate-strings-in-mysql – Keloo Aug 10 '17 at 17:17
  • Even if they are on multiple different tables? I have to make an association going from INVOICE to table APPLICATION which in turn links to PARK table. – sblumberg Aug 10 '17 at 17:32
  • [Little Bobby Tables](http://bobby-tables.com/) approves of this code. – bishop Aug 10 '17 at 19:00

1 Answers1

1

When adding it into an array, instead of...

if(in_array($applicationparkid,$allparkarray)){
    //if already contained in the allparkarray

} else {
    array_push($allparkarray,$applicationparkid);   
}

You can just write...

$allparkarray[$applicationparkid][]=$invoiceid;

The $allparkarray[$applicationparkid] just sets up the point at which the value is going to be added to. So the $applicationparkid is the ket used to associate the values. The [] bit just adds the value to the last element of the array . Or a new array of it's the first time.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • I'm not even going to begin how this even works in a single line of code. I am here overcomplicating everything and you come at me with just one line of code to replace my nasty ugly code. Thank you very much. – sblumberg Aug 10 '17 at 19:02
  • I should have explained it a bit better... I've updated the answer. – Nigel Ren Aug 10 '17 at 19:17