BlitZz
BlitZz

Reputation: 132

PHP: foreach loop with array_merge to create json objects

I'm new to php but i really like like it so far! Now i stumbled over a problem with array_merge.

This is my code, it simply grabs each table specified in my database then makes it to a big json file.:

  $tables = array("buildings", "medical", "other", "tools", "traps", "weapons"); 

    foreach ($tables as $table) {

        // Get tables from database
        $sth = $db->query("SELECT * FROM $table");
        $result = $sth->fetchAll();

        // Merge arrays together
        if ($arr === null) {
            //echo "its 0 <br/> ";
            $arr = array( "$table" => $result );
        } else {
            //echo "more than 0 <br/> ";
            $arr2 = array( "$table" => $result );
            $merge = array_merge($arr, $arr2);
        }

    } //End loop

    echo $merge;

So far It's working somehow, I manage to get the first table "buildings" and the last table "weapons" to be displayed the way i want perfectly!

But I don't understand why it jumps over the other ones.. I believe it has something to do with $arr2 and that i need to specify a unique array for each of the tables. But how can i achieve this? Is this the way to go or is there a more efficient way to achieve this? Thanks!

Upvotes: 0

Views: 1432

Answers (5)

Sverri M. Olsen
Sverri M. Olsen

Reputation: 13283

Your code can be greatly simplified.

$tables = array("buildings", "medical", "other", "tools", "traps", "weapons"); 
foreach ($tables as & $table) {
    $table = $db->query("SELECT * FROM $table")->fetchAll();
}
echo json_encode($tables);

Of course, you still need to check for errors (database and JSON errors), in order for the code to be robust, but the simpler you can make something the better. Simple is good, especially when it comes to programming.

Upvotes: 1

Kevin Schwerdtfeger
Kevin Schwerdtfeger

Reputation: 590

Its failing because of this line

    $merge = array_merge($arr, $arr2);

Your merge is always a merge of $arr (which is the first entry in $tables) and $arr2, which is the latest entry being processed. You are not merging the data with the previously merged data, such as

    $arr = array_merge($arr, $arr2)

Also, you can get rid of the if/else logic by just starting off by setting $arr to an empty array to start.

    $arr = array();

Upvotes: 1

JTC
JTC

Reputation: 3474

If I understand correctly...

Instead of this two lines

$arr2 = array( "$table" => $result );
$merge = array_merge($arr, $arr2);

You can try this:

$tables[$table] = $result;

Upvotes: 1

Muhammad Ahmed
Muhammad Ahmed

Reputation: 481

$tables = array("buildings", "medical", "other", "tools", "traps", "weapons"); 
$tableArr = array();
foreach ($tables as $table) {

    // Get tables from database
    $sth = $db->query("SELECT * FROM $table");
    $result = $sth->fetchAll();

    if(isset($result)){
        $tableArr[$table] = $result;
    }else{
        $tableArr[$table] = '';
    }


} //End loop

print_r($tableArr);

Created new array and set index as table name and store its result in that array.

Upvotes: 1

Guilherme Ferreira
Guilherme Ferreira

Reputation: 143

You can put everything inside an unique array, like:

$myArray["$table"] = $result;

Upvotes: 1

Related Questions