This question already has answers here:
Merging and group two arrays containing objects based on one identifying column value
(4 answers)
Closed 5 months ago.
I have 2 arrays that I want to merge by the id of the objects that in the arrays (2 different objects):
$object1 = new User();
$object2 = new User();
$object3 = new AdminUser();
$object4 = new AdminUser();
$object1->id = "1234";
$object1->name = "testUser1";
$object2->id = "34553";
//the id is like user1 but the username is different
$object3->id = "1234";
$object3->name = "testUser2";
$object4->id = "44234";
$array1 = [$object1,$object2];
$array2 = [$object3,$object1,$object4];
my wanted result is :
[$object1,$object2,$object4];
i tried :array_unique(array_merge($array1,$array2), SORT_REGULAR);
and I also tried :
$result = array_merge( $array1, $array2 );
$result = array_map("unserialize", array_unique(array_map("serialize", $result)));
but it didn't work
`
You can use array_reduce after merging those 2 arrays:
$mergedObjects = array_merge($array1, $array2);
$result = array_reduce($mergedObjects, function ($carry, $user) {
if (!isset($carry[$user->id])) {
$carry[$user->id] = $user;
}
return $carry;
}, array());
array functions are only ment for arrays, they do not parse anything inside objects that are within arrays. What you could do is push the object ids as array keys:
$mainArray = array();
foreach($objects as $object){ //or do it for every object if you create them manually
array_merge($mainArray, array($object->id => $object);
}
If the keys ( ids ) are duplicate, the latest takes priority.
Related
This question already has answers here:
How to cast array elements to strings in PHP?
(7 answers)
Closed last year.
I have a data array like this:
$data = array(1,2,3);
how to change the array like this:
$data = array('1','2','3');
Tanks Before
You have to change type of all array values, just like this
foreach ($data as $key => $val) {
$data[$key] = (string) $val;
}
or
function convert2string($n)
{
return (string) $n;
}
$a = [1, 2, 3];
$data = array_map('convert2string', $a);
This question already has answers here:
How can you make a multidimensional array unique? [duplicate]
(6 answers)
Closed 5 years ago.
I have an Array which has 3 Dimensions:
$data_ary[$k_0][$v_0[4]]['...'] = ...
In the brackets with '...' I write Parameters like "Country", "Designation", "Year" and so on. Now as I run this through a loop the values for $k_0 increases every loop by 1 and $v_0[4] always changes the specific value.
My Problem: Very often the values are duplicates across the different dimensions. For Example:
$data_ary[1][1]['Country'] = 'Germany';
$data_ary[1][1]['Year'] = '2017';
$data_ary[2][1]['Country'] = 'Germany';
$data_ary[2][1]['Year'] = '2017';
How do I delete those duplicates? I have tried array_unique() but the result was that from the 27.000 entries 26999 got deleted..
Some Example Input:
$data_ary[6]['GERMANY']['YEAR'] = 2017;
$data_ary[6]['GERMANY']['MONTH'] = 1;
$data_ary[6]['GERMANY']['ID'] = 6010;
$data_ary[6]['GERMANY']['COUNTRY'] = 'GERMANY';
$data_ary[7]['ITALY']['YEAR'] = 2016;
$data_ary[7]['ITALY']['MONTH'] = 4;
$data_ary[7]['ITALY']['ID'] = 52752;
$data_ary[7]['ITALY']['COUNTRY'] = 'ITALY';
$data_ary[8]['GERMANY']['YEAR'] = 2017;
$data_ary[8]['GERMANY']['MONTH'] = 1;
$data_ary[8]['GERMANY']['ID'] = 6010;
$data_ary[8]['GERMANY']['COUNTRY'] = 'GERMANY';
As you can See the first and the second are basically the same but differ by the value in the first bracktes.
You can do it like below:-
$input = array_map("unserialize", array_unique(array_map("serialize", $data_ary)));
print_r($input);
Output:- https://eval.in/903355
Reference:- How to remove duplicate values from a multi-dimensional array in PHP
This question already has an answer here:
Assign by reference bug
(1 answer)
Closed 6 years ago.
Consider the following PHP code segment.
$array1 = array(1,20);
$x = &$array1[1];
$array2 = $array1;
$array2[1] = 22;
print_r($array1[1]); // Output is 22
Here, $array2 is not referencing to $array1, but how to change value in $array2 by changing value of $array1?
If you want $array2 to be a reference of $array1 then you do the same thing as with $x.
$array2 = &$array1;
Now anything you change in either $array1 or $array2 is visible in both arrays since $array2 is just a reference to $array1.
Update
Thinking about it, what you may be looking at is a way to change a value, but still have a full copy of the arrays. This is doable with an object.
$obj = new stdClass();
$array1 = array(1, 20);
$array1[1] = $obj;
$array1[1]->color = 22;
$array2 = $array1;
$array2[1]->color = 33;
echo $array1[1]->color; // prints 33
This is because objects are always copied by reference, whereas numbers and strings are copied as is.
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 1 year ago.
I've got 6 arrays - 1 with name and 5 with some properties - which should be assigned to that name. All values are of course in order. I'd like to make a 2-dimensional array with will be later put into CSV and the result should be as on the table here:
I guess that i have to do 2 loops here, but I can't make them work. How to construct such array?
Solution found
I've connected all arrays:
$final_array = array($nazwa_array,$new_ilosc_array,$new_koszt_array,$new_cena_lifo_array,$new_cena_fifo_array,$new_rodzaj_array);
I've found a matrix transposition function, which returns array in correct order:
function transpose($array) {
array_unshift($array, null);
return call_user_func_array('array_map', $array);
}
$a = array();
foreach ( $names AS $key => $value ) {
$a[$key]['name'] = $value;
$a[$key]['property1'] = $value.'->'.$property1_array[$key];
$a[$key]['property2'] = $value.'->'.$property2_array[$key];
$a[$key]['property3'] = $value.'->'.$property3_array[$key];
$a[$key]['property4'] = $value.'->'.$property4_array[$key];
$a[$key]['property5'] = $value.'->'.$property5_array[$key];
}
This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 10 months ago.
This is really simple but I need a quick way to do this.
I have three arrays like
$a = array('a','b','c');
$p = array('p','q','r');
$x = array('x','y','z');
How do I combine them to make
array (
[0] => array ('a','p','x');
[1] => array ('b','q','y');
[2] => array ('c','r','z');
);
Wouldn't array_map(null, $a, $p, $x); be better?
See array_mapĀDocs.
<?php
$a = array('a','b','c');
$p = array('p','q','r');
$x = array('x','y','z');
$arr = array();
for($i=0; $i<count($a); $i++){
$arr[$i] = array($a[$i], $p[$i], $x[$i]);
}
?>
array_map is simpler, but for the sake of possibibility, a quickly typed code example to make use of the MultipleIterator to solve the issue:
$it = new MultipleIterator;
foreach(array($a, $p, $x) as $array) {
$it->attachIterator(new ArrayIterator($array));
}
$items = iterator_to_array($it, FALSE);
Might be handy in case it's more than an array.