How to merge two arrays of objects
-
A: Array ( [0] => gapiReportEntry Object ( [metrics:private] => Array ( [visits] => 1036 [pageviews] => 2046 [bounces] => 693 [entrances] => 1036 ) [dimensions:private] => Array ( [date] => 20110114 ) ) ) B: Array ( [0] => gapiReportEntry Object ( [metrics:private] => Array ( [goal1Completions] => 1 ) [dimensions:private] => Array ( [date] => 20110114 ) ) ) C: Array ( [0] => gapiReportEntry Object ( [metrics:private] => Array ( [visits] => 1036 [pageviews] => 2046 [bounces] => 693 [entrances] => 1036 [goal1Completions] => 1 ) [dimensions:private] => Array ( [date] => 20110114 ) ) ) I want to merge(A,B) and get C.
-
Answer:
To understand combining of array Take this example $arr1 = (1, 2, 3, 4, 5); $arr2 = (10, 20, 30, 40, 50); $arr3 = array_merge ($arr1 , $arr2 ); print_r($arr3); //(1,2,3,4,5,10,20,30,40,50) shuffle($arr3 );//(1,10,2,20,3,30,4,40,5,50)
biaodianfu at Stack Overflow Visit the source
Other answers
You can make use of the array_merge() function available in PHP <?php $array1 = array("color" => "red", 2, 4); $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4); $result = array_merge($array1, $array2); print_r($result); ?>
Zaje
I don't know the reason of such question, but here is solution: //lets think $A - A - array, $B - B array $a1 = (array)$A[0]; $b1 = (array)$B[0]; $c = array_merge($a1, $b1); print_r($result); //shows your Array, but not object. //You can create object using printed resulted array.
MDI
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.
MarcusJoe
For your specific case, let's be clear that you want to merge two arrays that are contained within objects contained within arrays. That being said, knowing how the resultant object was to be used (do you just need the data from those arrays, or does the rest of the object's instance matter?) would help give you an answer. tl;dr: $temparray = array_merge($A[0]->metrics, $B[0]->metrics); $C->methodToSetMetricsArray($temparray); Where the above methodToSetMetricsArray() does what it says, since the array is private and can't be set directly.
Crontab
Related Q & A:
- How to merge multiple CSV files into a single CSV file?Best solution by solveyourtech.com
- How to merge datasets in Stata conditionally?Best solution by kb.iu.edu
- How to merge 2 xml docs with xslt?Best solution by Stack Overflow
- How to merge shapefiles?Best solution by Geographic Information Systems
- Can a MongoDB key have two arrays?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.