Home » Php » php – While loop creating new array

php – While loop creating new array

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a for loop with thats running through an array of ids.

I want to get all the items for that id push it to an array. Then add that array to an object and add that object to another array. then repeat the process.

But it keeps returning a single array with all the items

 $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_container['item44'] = $item;
                        $array_container['acctID44'] = $acctID;


                    }
                    $final_array['objectContainer'] = (object) $array_container;    

                }
            }
             $jsonData = json_encode($final_array);
             echo $jsonData;

Looking for Something like this

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