First of all I'm not saying this is the end all of answers to this question, but it's been nine months and I feel the question hasn't been answered, while it does have it's uses.
From what I understand you have two (or more) arrays, consisting of multiple objects, who hold different attributes of the same object. As for instance in the next example:
ArrayedObjects1=array(
[0]=>stdClass Object
(
[id] => 1
[name] => foo
)
[1] => stdClass Object
(
[id] => 51
[name] => bar
)
)
ArrayedObjects2=array(
[0]=>stdClass Object
(
[length] => 195
[color] => blue
)
[1] => stdClass Object
(
[length] => 100
[name] => purple
)
)
Use cases could be for very large projects where multiple databases are used to store single values of the same object and simple join queries aren't possible.
These two arrays can be joined by using two functions I made, which simply allow for a unspecified amount of arrays to be joined together.
class FooBar {
public function mergeArrayedObjects(){
# We generate a new empty array to return from the function, to which we merge the existing arrays
$returnResponse=array();
# Then we establish the the number of arguments passed to the function
$numArgs=func_num_args();
# As objects are stored within an array of equal length, we loop through the first
for($i=0;$i<count(func_get_arg(0));$i++){
# For each key within the array we merge all existing arrays. We get this number from the number of arguments
# First we check if there already exists an entry into the returnRespone array with the current key. Two choices:
# Case 1 => no: create one by merging the first two arrays into this new array
# Case 2 => yes: merge the existing entry with the next array and recreate it
# BTW we correct $numArgs in the for-loop, as we add 1 to $j in the second case to exclude duplication of work
for($j=0;$j<($numArgs-1);$j++){
# Seems like you can't check on empty with func_get_arg(), so store it in a variable
# We do this as no merge is needed if the array is empty, or the key is empty
$isempty=func_get_arg($j+1);
if(!empty($isempty)||!empty($isempty[$i])){
if(!$returnResponse[$i]){
$array1=func_get_arg($j);
$array2=func_get_arg($j+1);
$returnResponse[$i]=self::mergeObjects($array1[$i],$array2[$i]);
}
else{
$array1=func_get_arg($j+1);
$returnResponse[$i]=self::mergeObjects($returnResponse[$i],$array1[$i]);
}
}
}
}
# Then we return the arrayed objects
return $returnResponse;
}
public function mergeObjects($obj1,$obj2){
# Function to merge objects
$merged_obj= (object) array_merge((array) $obj1, (array) $obj2);
return $merged_obj;
}
}
This returns the following array:
$joinArrays=new FooBar;
$joinedArray=$joinArrays->mergeArrayedObjects(ArrayedObjects1,ArrayedObjects2);
$joinedArray=array(
[0]=stdClass Object
(
[id] => 1
[name] => foo
[length] => 195
[color] => blue
)
[1] => stdClass Object
(
[id] => 51
[name] => bar
[length] => 100
[name] => purple
)
)
As I said this can be used for an unlimited amount of arrays. If there's a faster way, feel free to let me know. I needed this for joining the results from queries between different DB's so I just wrote to as this does the job and is quite flexible at the same time.
This can also be extended to check if one of the entries within the object is an array and do an array_merge ofcourse, it would add two lines of code.