Combine array and add key for each array - php

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];
}

Related

How to combine 2 arrays with the condition that 2 and 5 values will be from the second array?

There are two arrays:
$arrOne = [1, 3, 4];
$arrTwo = [2, 5,];
$newArr = [];
How to merge to get like this;
$newArr = [1, 2, 3, 4, 5];
Now displays through one, this option is not suitable.
foreach ($arrOne as $k => $v) {
$newArr[] = $v;
$newArr[] = $arrTwo[$k];
}
Here is another example. Tthe values can be different in the array.
$arrOne = [154, 32, 15];
$arrTwo = [682, 124,];
Again for this example it is necessary that the values from the second array always be at 2 and 5 positions in the new array:
$newArr = [154, 682, 32, 15, 124];
You can use array_splice to merge the arrays in the intended way to the specific indices:
array_splice( $arrOne, 1, 0, $arrTwo[0]);
array_splice( $arrOne, 4, 0, $arrTwo[1]);
var_dump($arrOne);
Demo 1:
Using original data:
$arrOne = [1, 3, 4];
$arrTwo = [2, 5,];
https://3v4l.org/dAMav
Demo 2:
Using second set of data:
$arrOne = [154, 32, 15];
$arrTwo = [682, 124,];
https://3v4l.org/Xhl3K
You can use array_merge and sort functions to achieve this as following:
$newArr = array_merge($arrOne, $arrTwo); // it will be [1, 3, 4, 2, 5]
sort($newArr); // it will sort array [1, 2, 3, 4, 5]
You can use $newArr now which have sorted data
$arrOne = [1, 3, 4];
$arrTwo = [2, 5];
$newArr = [];
foreach($arrOne as $key => $one) {
switch($key) {
case 1:
$newArr[] = $arrTwo[0];
break;
case 4:
$newArr[] = $arrTwo[1];
break;
}
$newArr[] = $one;
}
if(count($newArr) < 5) {
$newArr[] = $arrTwo[1];
}
Demo link
Here we are considering there will be always two elements in second array
$arrayOne = [12,23,45,56,65,78,99];
$arrayTwo = [2,5];
$mergedArray = [];
foreach($arrayOne as $key => $value) {
$position = $key + 1;
if ($position === 2) {
$mergedArray[] = $arrayTwo[0];
} else if ($position === 4) {
$mergedArray[] = $arrayTwo[1];
}
$mergedArray[] = $value;
}

how to merge an array

using the code below
$matchs = DiraChatLog::where('status','=','Match')->whereBetween('date_access', [$request->from, $request->to])->get();
foreach ($matchs as $key => $match) {
$array[] = [
$match->status => $match->date_access,
];
}
dd($array);
i get the output
now i want to merge the 4 arrays to 1.. how can i do that? my ouuput should be array:1> date => value
i have tried array_merge() and array_push() and it didnt work
for example using recursive array iterator from spl
$array = array(array(1,2,3),array(5,6,7),array(8,9,10));
$mergedArray = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach ($mergedArray as $a) {
echo $a; // [1,2,3,4,5,6,7,8,9,10]
}
and that should be enough.
here is how u can use array_merge().
$arr = array(
0 => array(1 ,2 ,3),
1 => array(4, 5, 6),
2 => array(7, 8, 9)
);
$allInOne = array();
foreach ($arr as $value) {
$allInOne = array_merge($allInOne, $value);
}
var_dump($allInOne); // the output [1,2,3,4,5,6,7,8,9]
This code will merge you all arrays in one this is just what you want i guess

How to merge arrays in PHP with a same value

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);
?>

PHP - merge multi-dimensional array based on keys

Suppose I have a multi-dimensional array:
Array
(
[0] => Array
(
[7,14] => 0.0,3.0
[5,11] => 0.0,5.0
[8,6] => 0.0,6.0
)
[1] => Array
(
[7,14] => 0.0,1.0
[5,11] => 1.0,3.0
[11,13] => 1.0,1.0
[6,8] => 1.0,0.0
)
I want to form a new array with keys as the union of all keys in each array and the values as the corresponding values of those unioned keys. If a key is ONLY found in one sub-array, then fill the would-be data with commas
i.e result ----------
Array
(
[7,14] => 0.0,3.0,0.0,1.0 // <--- union 0.0,3.0 and 0.0,1.0
[5,11] => 0.0,5.0,1.0,3.0
[8,6] => 0.0,6.0,,
[11,3] => 1,0,1.0,,
[6,8] => 1.0,0.0,,
)
Here's what I've tried. I'm pretty close to the right answer!
function combineValues($bigArray){
$combinedArray = array();
for($i = 0; $i < (count($bigArray) - 1); $i++) {
$keys = array_keys($bigArray[$i]);
for($j = 0; $j < count($keys); $j++){
$currentKey = $keys[$j];
if (isset($bigArray[$i+1][$currentKey]){
$combinedArray[$currentKey] = $bigArray[$i][$currentKey] + "," + $bigArray[$i+1][$currentKey];
} else {
}
}
}
return $combinedArray;
}
It is easy to use foreach instead of for.
$array2 = [[1, 2, 3, 4], [2 => 7, 8, 9]];
$return_array = array();
foreach ($array2 as $array1)
{
foreach ($array1 as $key => $value)
{
if (isset($return_array[$key]))
$return_array[$key].=',' . $value;
else
$return_array[$key] = $value;
}
}
var_dump($return_array);
Also you could use functional features of language. array_walk_recursive
$array2 = [[1, 2, 3, 4], [2 => 7, 8, 9]];
$return_array = array();
array_walk_recursive($array2, function ($value, $key)use(&$return_array)
{
if (isset($return_array[$key]))
$return_array[$key].=',' . $value;
else
$return_array[$key] = $value;
});
var_dump($return_array);

php -Merging an Array

I have two array which i want to merge in a specific way in php.
So i need your help in helping me with it as i tried and failed.
So say i have two arrays:
$array1= array(
"foo" => 3,
"bar" => 2,
"random1" => 4,
);
$array2= array(
"random2" => 3,
"random3" => 4,
"foo" => 6,
);
Now when during merging i would like the common key's values to be added.
So like foo exists in array1 and in array2 so when merging array1 with array 2 i should get "foo" => "9"
I better illustration would be the final array which looks like this:
$array1= array(
"foo" => 9,
"bar" => 2,
"random1" => 4,
"random2" => 3,
"random3" => 4,
);
So again i would like the values of the common keys to be added together and non common keys to be added to array or a new array
I hope i was clear enough
Thanks,
Vidhu
Something like that:
function mergeValues() {
$result = array();
$arraysToMerge = func_get_args();
foreach ($arraysToMerge as $array) {
foreach($array as $key => $value) {
$result[$key] += $value;
}
}
return $result;
}
$res = mergeValues($array1, $array2, $array3); // Can pass any ammount of arrays to a function.
foreach($array1 as $k => $v)
{
If (isset($array2[$k]))
$array1[$k] += $array2[$k];
}
foreach($array2 as $k => $v)
{
If (!isset($array1[$k]))
$array1[$k] = $array2[$k];
}

Categories