Home » Php » PHP overwrite array mysql query?

PHP overwrite array mysql query?

Posted by: admin February 25, 2020 Leave a comment

Questions:

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

[
  {"ids":["id1",'id2','id3'],
  "item":["name","name2","name3"]},
  {"ids":["id4",'id5','id6'],
  "item":["name4","name5","name6"]},
  {"ids":["id1114",'id1115','id1116'],
  "item":["name11114","name11115","name1116"]},
]

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

//get customers of single item
            if($this->connect()->connect_error){
                echo die("Connection failed: " . $this->connect()->connect_error);
            }else{
                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: