I want to merge two arrays with the same value -> user_id to one array.
You can see I have two arrays with different objects. I want merge them by user_id to one array with all objects.
For example:
$array1 = [
$array[0]->leads = 5643;
$array[0]->user_id= 15;
$array[0]->sales = 1433;
$array[1]->leads = 3264;
$array[1]->user_id= 9;
$array[1]->sales = 1254;
];
$array2 = [
$array[0]->user_id= 15;
$array[0]->processing = 2300;
$array[0]->deleted = 203;
$array[1]->user_id= 9;
$array[1]->processing = 103;
$array[1]->deleted = 80;
];
The following array is the target.
$result = [
$array[0]->user_id= 15;
$array[0]->processing = 2300;
$array[0]->leads = 5643;
$array[0]->deleted = 203;
$array[0]->sales = 1433;
$array[1]->user_id= 9;
$array[1]->processing = 103;
$array[1]->leads = 3264;
$array[1]->deleted = 80;
$array[1]->sales = 1254;
];
Iterate your array and build the combined array of objects using user_id as the key.
foreach ($array as $entry) {
// if an entry for this user id hasn't been created in the result, add this object
if (!isset($result[$entry->user_id])) {
$result[$entry->user_id] = $entry;
// otherwise, iterate this object and add the values of its keys to the existing entry
} else {
foreach ($entry as $key => $value) {
$result[$entry->user_id]->$key = $value;
}
}
}
Given the additional info you just gave (two separate arrays) the solution is basically the same, just merge the two arrays together first.
foreach (array_merge($array1, $array2) as $entry) { ...
(Working example at https://3v4l.org/ccdQc)
How about array_merge,
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
or array_merge_recursive,
<?php
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
?>
Example
$array = [
1 => [
'cost' => '25'
],
2 => [
'blah' => 'test'
]
];
$new_array = array_merge_recursive($array[1], $array[2]);
var_dump($new_array);
Output
array(2) {
["cost"]=>
string(2) "25"
["blah"]=>
string(4) "test"
}
Live Example
Repl
<?php
$array1= array("leads"=> 5643,"user_id"=>15,"sales" =>1433);
$array2= array("user_id"=> 15,"processing" => 2300,"deleted" => 203);
$array= (array_merge($array1,$array2));
print_r($array);
?>
Related
I just can't find the right question for this
pretty much merging array From this
$arr1 = ["temp1", "temp2", "temp3"];
$arr2 = [5, 7, 2];
to this
$combined = [["name" => "temp1", "number" => 5], ["name" => "temp2", "number" => 7], ["name" => "temp3", "number" => 2]];
any idea to do it in most efficient way other than foreach?
$arr1 = ["temp1", "temp2", "temp3"];
$arr2 = [5, 7, 2];
foreach($arr1 as $key => $value)
{
$r[$key]['name'] = $value;
$r[$key]['number'] = $arr2[$key];
}
print_r($r);
Built-in function array_map actually can work with multiple arrays:
$result = array_map(function($value1, $value2) {
return ["name" => $value1, "number" => $value2];
}, $arr1, $arr2);
Here are some benchmarks comparing with simple foreach
You can use a foreach loop,
$result = [];
foreach($arr1 as $key => $value){
$resutl[] = array("name"=>$value,"number"=>$arr2[$key]);
}
You can do the following code to get your result
$arr1 = ["temp1", "temp2", "temp3"];
$arr2 = [5, 7, 2];
$count = count($arr1);
$combined = array();
for($i=0;$i<$count;$i++){
$combined[$i]['name'] = $arr1[$i];
$combined[$i]['number'] = $arr2[$i];
}
I want to go from these arrays:
$array1 = ["x", "y", "z"];
$array2 = ["a","b"];
$array3 = ["1","2","3","4","5","6"];
To this array:
$arrayResult =
array(
array("x" => array("a" => array(1,2,3,4,5,6),
"b" => array(1,2,3,4,5,6)),
"y" => array("a" => array(1,2,3,4,5,6),
"b" => array(1,2,3,4,5,6)),
"z" => array("a" => array(1,2,3,4,5,6),
"b" => array(1,2,3,4,5,6)))
);
I tried to make this combined array with cartesian product approaches, but no satisfying result so far.
Here is another solution without using any loop:
$array1 = ["x", "y", "z"];
$array2 = ["a","b"];
$array3 = ["1","2","3","4","5","6"];
$result = array_combine(
$array1,
array_fill(
0,
count($array1),
array_combine(
$array2,
array_fill(0, count($array2), $array3)
)
)
);
print_r($result);
Here is the demo
use array_fill_keys twice to get the result
$result = array_fill_keys(
$array1,
array_fill_keys($array2, $array3)
);
Demo on eval.in
I have two arrays.
I need to make a foreach where it loops the values that is the same in both arrays. The arrays are not in same order and one of the arrays have more values than the other array.
I could do this.
foreach($array1 as $items1)
{
foreach($array2 as $items2)
{
if($items1 == $items2)
Echo "Match!";
}
}
But that takes a lot of time to load
Edit
I dont get any matches.
Array 1
$array1 = array();
while($fetch = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
{
$array1[] = array("a" => $fetch['value']);
}
Array 2
$filename = "test.txt";
$fp = #fopen($filename, 'r');
if ($fp) {
$array2 = explode("\n", fread($fp, filesize($filename)));
}
CODE
array_unshift($array2,"b");
$result = array_intersect($array1, $array2);
print_r($result);
You can use array_intersect:
$matches = array_intersect($array1, $array2);
You can use PHP in built function array_intersect()
<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
?>
Output
Array
(
[a] => green
[0] => red
)
Note : Returns an array containing all of the values in array1 whose values exist in all of the parameters.
I have two numeric arrays (these arrays will always have the same number of keys and values).
$array1 = array(0 => "key1", 1 => "key2");
$array2 = array(0 => "value1", 1 => "value2");
$array_final = array(value of the $array1 => $value of the array2);
If I write a while and fill the $array_final, it only fills with the last key and value
So it's like:
for ($i = 0; $i < count($array(1))
{
$array_final = array($array1[$i] => $array2[$i]);
}
$array_final = array("key2" => "value2");
But I want:
$array_final = array("key1" => "value1", "key2" => "value2");
You want array_combine
It does exatcly what you need
So basically
$array1 = array(0 => "key1", 1 => "key2");
$array2 = array(0 => "value1", 1 => "value2");
$array_final = array_combine($array1, $array2);
for ($i=0; $i< sizeof($array1) && $i< sizeof($array2) ; $i++)
{
$array_final[$array1[$i]] =$array2[$i];
}
You may try below code.
$array1 = array(0 => "key1", 1 => "key2");
$array2 = array(0 => "value1", 1 => "value2");
$array_final = combine_if_same_keys($array1,$array2);
print_r($array_final);
function combine_if_same_keys( $array_one, $array_two ) {
$expected = false;
ksort($array_one);
ksort($array_two);
$diff = array_diff_key($array_one, $array_two);
if( empty($diff) && count($array_one) == count($array_two) ) {
$expected = array_combine( $array_one, $array_two );
}
return $expected;
}
I have two arrays like this:
$array1 = [
[10, 'Some Name..'],
[11, 'Some Name..'],
[13, 'Some Name..'],
];
$array2 = [
[13, 'Viewed']
];
How can I merge these two arrays without looping? Is there any php functionality available for this? I need this kind of output:
[
[10, 'Some Name..'],
[11, 'Some Name..'],
[13, 'Some Name..', 'Viewed']
]
You can use the PHP function array_merge_recursive.
See the example:
<?php
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
?>
There will not be a non-loop way to accomplish this. Maybe you are seeking function-based syntax, but for this scenario I think this will only introduce unnecessary convolution.
For best efficiency, generate a result array that doubles as a lookup array. If the key already exists in the result array, you only need to push the [1] value into that row. If you wish to reindex the result, just call array_values() on it.
Code: (Demo)
$array1 = [
[10, 'Some Name..'],
[11, 'Some Name..'],
[13, 'Some Name..'],
];
$array2 = [
[13, 'Viewed']
];
$result = [];
foreach (array_merge($array1, $array2) as $row) {
if (!isset($result[$row[0]])) {
$result[$row[0]] = $row;
} else {
$result[$row[0]][] = $row[1];
}
}
var_export($result);
Functional style: (Demo)
var_export(
array_reduce(
array_merge($array1, $array2),
function($result, $row) {
if (!isset($result[$row[0]])) {
$result[$row[0]] = $row;
} else {
$result[$row[0]][] = $row[1];
}
return $result;
}
)
);