Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8796511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:38:25+00:00 2026-06-13T23:38:25+00:00

I am trying to add new arrays to an existing array dynamically from a

  • 0

I am trying to add new arrays to an existing array dynamically from a database but my loop is only adding the last row from mysql_fetch_row. I think it is actually overwriting the same array.

PHP Code

<?php
$con = require_once('./dbconnect.php');

global $con;
mysql_select_db("packages", $con);
$packages = mysql_query("SHOW TABLES FROM packages");

while($row = mysql_fetch_row($packages)){
    $node = array();

    foreach($row as $key2 => $value2){
        $node[$row[0]] = array("Item1" => "Other dynamic Info here");
    }
}

print_r($node);

mysql_close($con);
?>

The output is as follows:

Array
(
    [Pack1] => Array
        (
            [Item1] => Other dynamic Info here
        )

)

It should be outputting:

Array
(
    [Pack1] => Array
        (
            [Item1] => Other dynamic Info here
        )

)
Array
(
    [Pack2] => Array
        (
            [Item2] => Other dynamic Info here
        )

)

I have been trying to get this foreach() loop to work for about a day now…what am i doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:38:26+00:00Added an answer on June 13, 2026 at 11:38 pm

    Declare

    $node = array();
    

    outside the while loop.

    If indexing of the node array is irrelevant you can just use the “append” notation

    foreach($row as $key2 => $value2){
        node[] = array("Item1" => "Other dynamic Info here");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im new to asp.net mvc. I'm trying add new model class but it got
I am trying to add a new column into an SQLiteDatabase but every time
Im trying to add an element to a database and then return a new
I'm trying to use an array to add a click listener to an existing
I'm trying to expand and add a new object to a array inside a
I am trying to push new array item into existing array variable that has
Im trying to add new class to existing project. My class work corectly when
I am trying to add a new service method to my existing WCF web
On an Update, I'm trying to create a new array element or add to
Im trying to put arraylists into an arraylist. Adding data to the new arrays,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.