0

How to store data from MySQL to a multidimensional array in PHP? This ini My database My database

I want to store every this database to multidimensional array like this

tId = array('Bid'=>'Brg', 'Bid'=>'Brg', 'Bid'=>'Brg');

i have this this code but its doesnt work

 $this->db->query($query);
    $transactions = array();
    while($row=$this->db->fetchAll()){
        $transactionId = $row['id'];
        $productId = $row['produk_id'];
        $product = $row['produk'];
       $transactions[$transactionId]=array($productId, $product);
    }
M K Wiro
  • 75
  • 6
  • How many iterations do you expect `fetchAll()` to make? https://www.php.net/manual/en/pdostatement.fetchall.php – mickmackusa Jan 07 '20 at 05:30
  • @YourCommonSense has a tidy menu to peruse: https://phpdelusions.net/pdo/fetch_modes#FETCH_UNIQUE – mickmackusa Jan 07 '20 at 05:35
  • Try this. $data = array(); while($row=$this->db->fetchAll()){ $transactionId = $row['id']; $productId = $row['produk_id']; $product = $row['produk']; $transactions[$transactionId]=array($productId, $product); array_push($data,$transactions); } – BukhariBaBa Jan 07 '20 at 05:41
  • @mickmackusa I expect 10 iterations – M K Wiro Jan 07 '20 at 05:57
  • `fetchAll()` only provides 1 ... "All" – mickmackusa Jan 07 '20 at 05:58
  • @mickmackusa i have 2 function in core, fetchAll and fetch. fetchAll() with return $this->stmt->fetchAll(PDO::FETCH_ASSOC); and fetch() with return $this->stmt->fetch(PDO::FETCH_ASSOC); so what must i make to make iteration for every Brg in sama Tid? – M K Wiro Jan 07 '20 at 06:04
  • You can loop with `fetch()` and manually construct the desired output array, but PDO offers some awesome techniques (as shown in my duplicate link and the link to phpdelusions). Please take a moment to actually read what I have directed you to. Closing your question is not a punishment, I am referring you to excellent pre-existing resources. – mickmackusa Jan 07 '20 at 06:07

0 Answers0