views:

471

answers:

3

Hi there I execute 2 query's on 2 diffrend servers with the same table structure. How can I merge the 2 Arrays in PHP?

Thanks

A: 

Take a look at array_merge

Byron Whitlock
A: 

Please keep in mind, when You query two database servers, You will have a collision between the id's of the records (db auto_increment feature) unless You're using UUID for each record identification.

So when You're not using uuid for the generation of ids of the records, when You merge the two arrays with results, you should mark wchich one is from with server to avoid collisions later.

astropanic
+2  A: 

PDO::query() returns a PDOStatement object which implements the interface Traversable.
The SPL class IteratorIterator wraps any Traversable to act like an Iterator.
And another SPL class, AppendIterator, can "concatenate" Iterators to act like one single iterator.

<?php
$result1 = $pdo1->query('SELECT * FROM foo');
$result2 = $pdo2->query('SELECT * FROM foo');

$it = new AppendIterator;
$it->append(new IteratorIterator($result1));
$it->append(new IteratorIterator($result2));

foreach($it as $row) {
  echo join(', ', $row), "\n";
}
VolkerK