Merge two multidimensional arrays, preserve numeric keys, and combine values inside array - php

I have two arrays and would like to combine / merge / put them together.
$arr1 = array(
0 => array(1, 2),
1 => array(5, 6)
);
$arr2 = array(
0 => array(2, 3),
1 => array(6, 7)
);
come_together_right_now($arr1, $arr2); // the missing function?
and the result would be:
Array (
[0] => Array (
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array (
[0] => 5
[1] => 6
[2] => 7
)
There are way too many array functions! array_merge and array_combine and the recursive alternatives seem to replace the values and they don't preserve numeric keys. How do I do this?

Assuming that they will always have the same keys!
$result = array();
foreach($arr1 as $key=>$array) {
$result[$key] = array_merge($array, $arr2[$key]);
}

I might be late of answering this question but this might help you simply using array_map,array_merge and array_unique function like as
$result = array_map('array_unique',array_map('array_merge',$arr1,$arr2));
print_r($result);
Output
Array (
[0] => Array (
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array (
[0] => 5
[1] => 6
[2] => 7
)
Demo

Synchronously iterate the arrays to access their rows.
Merge rows, remove duplicates, then re-index the elements.
Code: (Demo)
var_export(
array_map(
fn(...$rows) => array_values(array_unique(array_merge(...$rows))),
$arr1,
$arr2
)
);

Related

Re-order 2-dimensional array using keys and values from one-dimensional array with php [duplicate]

This question already has an answer here:
PHP Find value in a multi-dimensional array by key
(1 answer)
Closed 1 year ago.
i am having a 2-dimensional array $a like this:
Array (
[1] => Array (
[id] => 19
[name] => SomeName
),
[2] => Array (
[id] => 23
[name] => AnotherName
),
[3] => Array (
[id] => 45
[name] => OneMoreName
)
)
And the second array $b like this:
Array (
[0] => 45
[1] => 23
[2] => 19
)
The values of 2nd array are id's from first array. The order of elements is right in 2nd array, but is not right in 1st. So, i want to re-order elements in 1st array to have finally:
Array (
[1] => Array (
[id] => 45
[name] => OneMoreName
),
[2] => Array (
[id] => 23
[name] => AnotherName
),
[3] => Array (
[id] => 19
[name] => SomeName
)
)
Of course, the number of elements can be more than 3, but in both arrays it is always the same. What standart php function or what approach can i use to do this?
You could use some array_walk function, but to be more understandable, i would use a specific algorithm:
$array1 = [
['id' => 19, 'name' => 'name19'],
['id' => 23, 'name' => 'name23'],
['id' => 45, 'name' => 'name45'],
];
$array2 = [45, 23, 19];
$newArray1 = [];
foreach ($array1 as $data){
// Get the array key from array2 with strict comparison value
$newKey = array_search($data['id'], $array2, true);
// Set it in a new array temporary
$newArray1[$newKey] = $data;
}
$array1 = $newArray1;
This way is quite short and easily understandable by someone else
One simple way:
$result = array_merge(array_flip($b), array_column($a, null, 'id'));
Index $a on the id and flip $b to get the values as the index
Merge the existing array into the ordered one, which will replace in the proper order
If you need to re-index then use array_values:
$result = array_values(array_merge(array_flip($b), array_column($a, null, 'id')));
I didn't test it but you could probably use array_intersect_key instead of array_merge.

how to transform two arrays in php, could this be possible?

how to transform two arrays. could this be possible?
to make arrays individually in php. how to transform two arrays. could this be possible?
to make arrays individually in php.
Arrayone
(
[0] => H00
[1] => T00.0
[2] => L00
)
Arraytwo
(
[0] => 1
[1] => 2
[2] => 3
)
Transform to like this
Array
(
[icd] => H00
[rank] => 1
)
Array
(
[icd] => T00.0
[rank] => 2
)
Array
(
[icd] => L00
[rank] => 3
)
Assume you want to have an array containing all the transformed arrays inside.
$array1 = array('H00','T00.0','L00');
$array2 = array('1','2','3');
$result = array();
$array3 = array_combine($array2, $array1);
foreach($array3 as $key => $value)
$result[] = array('icd' => $value, 'rank' => $key);
print_r($result);
Try this..
You can able to combine two arrays in PHP.
$arr3 = array_combine($arr2, $arr1);
print_r($arr3);

Need common arrays from two multidimensional arrays

Hi I have below multidimensional arrays -
Array
(
[user_attempts] => 0
[2] => Array
(
[0] => 1
[1] => 4
)
[3] => Array
(
[0] => 32
[1] => 23
)
[4] => Array
(
[0] => asdsa
)
[1] => Array
(
[0] => Earth
)
)
and my second array is like below
Array
(
[1] => Array
(
[0] => Earth
)
[2] => Array
(
[0] => 2
[1] => 3
)
[3] => Array
(
[0] => 32
[1] => 23
)
[4] => Array
(
[0] => asdsa
[1] => asdas
)
)
I need to intersect both multidimensional arrays - so the result would be
Array
(
[1] => Array
(
[0] => Earth
)
[3] => Array
(
[0] => 32
[1] => 23
)
)
Can anyone help me to sort this out.
What I have tried is using array_intersect() but it limits to single array not multidimensional i guess.
PHP comes with a ton of functions already built in, but sometimes you still have to implement things yourself. What you want to do can be easily done by using the existing functions.
The goal is to do the following steps:
Find the keys that exist in both arrays
Loop through the array using these keys
Take the items of both input arrays with each of these keys
Calculate the intersection of those two arrays
Put it into a result array
Here is one way to do this:
function array_intersect_2dim (array $a1, array $a2) {
$keys = array_intersect(array_keys($a1), array_keys($a2));
$return = array();
foreach ($keys as $key) {
$return[$key] = array_intersect($a1[$key], $a2[$key]);
if (sizeof($return[$key]) == 0) {
unset($return[$key]);
}
}
return $return;
}
It works only for two dimensions. If you need more, you have to build a recursive approach, which follows the exact same principle.
To make the easier to compare you can use serialize/unserialize on this one. And then use array_intersect(). Try this example: Sample Output
$array1 = array( 'user_attemps' => 0, 2 => array(1, 4), 3 => array(32, 23), 4 => array('asdsa'), 1 => array('Earth'),);
$array2 = array( 1 => array('Earth'), 2 => array(2, 3), 3 => array(32, 23), 4 => array('asdsa', 'asdas'),);
$result = array_map('unserialize',array_intersect(array_map('serialize', $array1), array_map('serialize', $array2)));
print_r($result);

PHP: combining same-length arrays into a multidimensional array where both end up as values (not keys)?

I have two same-length arrays like this:
Array
(
[0] => a
[1] => b
[2] => c
)
Array
(
[0] => 1
[1] => 2
[2] => 3
)
And I want to end up with this:
Array
(
[0] => Array
(
[0] => a
[1] => 1
)
[1] => Array
(
[0] => b
[1] => 2
)
[2] => Array
(
[0] => c
[1] => 3
)
)
array_combine would make one set of the above values into array keys, which I don't want -- I want both to end up as array values, combining each item of the two arrays into a new array.
Is there a built in function to do this or do I have to roll my own?
Try this:
$result = array();
foreach ($array1 as $i => $val) {
$result[] = array($val, $array2[$i]);
}
http://codepad.viper-7.com/Jx5H1Q
Is there a built in function to do this
Yes
or do I have to roll my own?
No
By calling array_map() and feeding it null as the callback parameter, then feeding it 2 or more arrays, it will restructure your data as desired.
Code: (Demo)
$array1 = ['a', 'b', 'c'];
$array2 = [1, 2, 3];
var_export(array_map(null, $array1, $array2));
Output:
array (
0 =>
array (
0 => 'a',
1 => 1,
),
1 =>
array (
0 => 'b',
1 => 2,
),
2 =>
array (
0 => 'c',
1 => 3,
),
)
If you had string keys, you could use array_merge_recursive to merge them. As it is, though, you'll need to do something else. For instance:
$result = Array();
$arrays = Array($array1,$array2...);
foreach($arrays as $arr) {
foreach($arr as $k=>$v) $result[$k][] = $v;
}

matching sub arrays on array and count them

i am having an array
Array
(
[1] => Array
(
[0] => 1
[1] => 3
)
[2] => Array
(
[0] => 1
[1] => 2
)
[3] => Array
(
[0] => 1
[1] => 3
)
)
and i need to find the common subarrays
In the above example array 1 and 3 have the common sub array
(
[0] => 1
[1] => 3
)
So the final array must be
Array
(
[1] => Array
(
[0] => 1
[1] => 3
)
[2] => Array
(
[0] => 1
[1] => 2
)
)
But i need to count the common values some how.
Any suggestion.
I wouldn't use this for production code, but here's a quick & somewhat clever way to do it:
$arrays = array(array(1,3), array(1,2), array(1,3)); // Your example data
$serialized = array_map('serialize', $arrays);
$counts = array_count_values($serialized);
foreach ($counts as $data => $count) {
echo "$count: " . print_r(unserialize($data), true);
}
Just compare each element of array with other assuming them as a linear array but use array_diff to compare each element. If they are different copy the element or array index into another array
To isolate unique rows, use the SORT_REGULAR flag with array_unique().
To acquire the number of duplicated rows, subtract the unique count from the initial count.
Code: (Demo)
$array = [[1, 3], [1, 2], [1, 3]];
$count = count($array);
$unique = array_unique($array, SORT_REGULAR);
echo "Number of duplicated rows: " . ($count - count($unique));

Categories