Home » Php » PHP overwrite array mysql query?

PHP overwrite array mysql query?

Posted by: admin February 25, 2020 Leave a comment


i have this code and i want to return an array of object and inside that object an associative array.
something like this


it sends the objects with the array but it only has the last element in the array

//get customers of single item
                echo die("Connection failed: " . $this->connect()->connect_error);
                for($i=0; $i<count($final_array['item']); $i++){
                    $single = $final_array['item'][$i];
                    $sql = "SELECT ACCT_ID, PARTS_EXPIRY, dcii_item, dcii_desc, dcii_acct_id, dcii_qty AS QTY 
                    FROM dos_account AS DOS
                    JOIN dos_client_invoice_items as DOS2
                    ON DOS.ACCT_ID  = DOS2.dcii_acct_id
                    WHERE dcii_item = '$single'
                    AND DOS.PARTS_EXPIRY BETWEEN '$start'  AND  '$end'
                    ORDER BY DOS.PARTS_EXPIRY";

                    $result = $this->connect()->query($sql);
                    $numRows = $result->num_rows; 
                    if($numRows > 0){
                          // $array_container = [];
                        while($row = $result->fetch_assoc()){

                            $item[] = $row['dcii_item'];
                            $acctID[] = $row['ACCT_ID'];

                            $array_container5 = array();
                            $array_container5[] = $row['dcii_item']; 
                            $array_container = array();
                            $array_container[] = $row['ACCT_ID']; 
                            //array_push($array_container, $row['dcii_item']);

                           $array_container2['item'] = $array_container5;
                           $array_container2['ids'] = $array_container;
                           $final_array['objectContainer'][] =  $array_container2;      

                 $jsonData = json_encode($final_array);
                 echo $jsonData;
How to&Answers: