How to concatenate arrays element recursively [duplicate] - php

This question already has answers here:
Joining similar data from two indexed arrays
(2 answers)
Closed 5 months ago.
I'm working on a project and I'm stacked since 2 days, this is my problem: I have two arrays and want to retrieve the second item in each object in Array_2 and concatenate it to the content of each object in first Array_1 in PHP.
Array_1
[[1453274700000,24011],[1453275000000,24222],[1453275300000,24284],[1453275600000,24331],...]
Array_2
[[1453274700000,51951],[1453275000000,52093],[1453275300000,52251],[1453275600000,52288],...]
Wanted_array
[[1453274700000,24011,51951],[1453275000000,24222,52093],[1453275300000,24284,52251],[1453275600000,24331,52288]...]

A functional solution:
$result = array_map(function (array $a1, array $a2) {
return array_merge($a1, [$a2[1]]);
}, $array_1, $array_2);
This assumes that all items are in order and only need to be merged by their order, not by their first value.

If you want $item[0] to define what "group" each value belongs to, you can iterate through the first array and save $item[0] as the key and $item[1] as the value. Do the same for the second array. Now iterate through the saved array for array1, and check if the saved array for array2 contains the same keys. Do the same for array2 (in case it has key that array1 doesn't have), and save it all to a new array:
<?php
$arr1 = array(
array('1453274700000',24011),
array('1453275000000',24222),
array('1453276000000',24222), // inexistent in $arr2
);
$arr2 = array(
array('1453275000000',52093),
array('1453274700000',51951),
array('1453273000000',24222), // inexistent in $arr1
);
$arr1dictionary = [];
$arr2dictionary = [];
$result = [];
foreach ($arr1 as $collection) {
$arr1dictionary[$collection[0]] = $collection[1];
}
foreach ($arr2 as $collection) {
$arr2dictionary[$collection[0]] = $collection[1];
}
foreach ($arr1dictionary as $key => $value) {
if (isset($arr2dictionary[$key])) {
$result[$key] = [$key, $value, $arr2dictionary[$key]];
} else {
$result[$key] = [$key, $value, null];
}
}
foreach ($arr2dictionary as $key => $value) {
if (isset($result[$key])) {
continue;
}
$result[$key] = [$key, null, $value];
}
$result = array_values($result);
print_r($result);
Output:
Array
(
[0] => Array
(
[0] => 1453274700000
[1] => 24011
[2] => 51951
)
[1] => Array
(
[0] => 1453275000000
[1] => 24222
[2] => 52093
)
[2] => Array
(
[0] => 1453276000000
[1] => 24222
[2] => (null, the value only exists in $arr1)
)
[3] => Array
(
[0] => 1453273000000
[1] => (null, the value only exists in $arr2)
[2] => 24222
)
)
DEMO

Use array_walk and add second item from $array2 if it exists.
$array1 = array(
array(1453274700000,24011),
array(1453275000000,24222),
array(1453275300000,24284),
array(1453275600000,24331)
);
$array2 = array(
array(1453274700000,51951),
array(1453275000000,52093),
array(1453275300000,52251),
array(1453275600000,52288),
);
array_walk($array1, function(&$item, $key) use ($array2){
if(isset($array2[$key][1])){
$item[] = $array2[$key][1];
}
});
print_r($array1);
Output
Array
(
[0] => Array
(
[0] => 1453274700000
[1] => 24011
[2] => 51951
)
[1] => Array
(
[0] => 1453275000000
[1] => 24222
[2] => 52093
)
[2] => Array
(
[0] => 1453275300000
[1] => 24284
[2] => 52251
)
[3] => Array
(
[0] => 1453275600000
[1] => 24331
[2] => 52288
)
)
EDIT
As #h2ooooooo pointed out that there could be possibility that array items are in random order. If array items can be in random order and they are matched with first index value, use this (works with PHP >= 5.5.0):
$array1 = array(
array(1453274700000,24011),
array(1453275000000,24222),
array(1453275300000,24284),
array(1453275600000,24331),
array(1453276000000,24222) // no match in $array2
);
$array2 = array(
array(1453275000000,52093),
array(1453274700000,51951),
array(1453275300000,52251),
array(1453275600000,52288),
);
array_walk($array1, function(&$item, $key) use ($array2){
// Find match in $array2
$array2_key = array_search($item[0], array_column($array2, 0));
// If match found
if($array2_key !== false && isset($array2[$array2_key][1])){
$item[] = $array2[$array2_key][1];
}
// No match
else{
$item[] = null;
}
});
print_r($array1);
OUTPUT
Array
(
[0] => Array
(
[0] => 1453274700000
[1] => 24011
[2] => 51951
)
[1] => Array
(
[0] => 1453275000000
[1] => 24222
[2] => 52093
)
[2] => Array
(
[0] => 1453275300000
[1] => 24284
[2] => 52251
)
[3] => Array
(
[0] => 1453275600000
[1] => 24331
[2] => 52288
)
[4] => Array
(
[0] => 1453276000000
[1] => 24222
[2] =>
)
)

Related

merging subarrays of multidimensional arrays while sorting array alphabetically

Trying to alphabetically merge arrays with foreach loop.
<?php
$fruits = array(
'Apple' => array('ids'=>array(1,2)),
'Banana' => array('ids'=>array(3,4)),
'Ananas' => array('ids'=>array(5,6))
);
$result = array();
foreach ($fruits as $name=>$subarr) {
$first_letter = mb_substr($name, 0, 1);
$result[$first_letter] = $subarr;
}
print_r($result);
gives me smth like
Array
(
[A] => Array
(
[ids] => Array
(
[0] => 5
[1] => 6
)
)
[B] => Array
(
[ids] => Array
(
[0] => 3
[1] => 4
)
)
)
instead of smth like
[A] => Array
(
[ids] => Array
(
[0] => 1
[1] => 2
[2] => 5
[3] => 6
)
)
how can I fix it?
You overwrite your result every iteration in this line:
$result[$first_letter] = $subarr;
Just create a new array if the subArray in the result array doesn't exists and merge the ids subArray into your result array.
foreach ($fruits as $name=>$subarr) {
$first_letter = mb_substr($name, 0, 1);
if(!isset($result[$first_letter]))
$result[$first_letter] = [];
$result[$first_letter] = array_merge($result[$first_letter], $subarr["ids"]);
}
Please try to use foreach loop.
$fruits = array(
'Apple' => array('ids'=>array(1,2)),
'Banana' => array('ids'=>array(3,4)),
'Ananas' => array('ids'=>array(5,6))
);
$result = array();
foreach ($fruits as $name=>$subarr) {
$first_letter = mb_substr($name, 0, 1);
foreach($subarr as $key=>$value){
foreach ($value as $gkey => $gvalue) {
$result[$first_letter]['ids'][] = $gvalue;
}
}
}
echo "<pre>";
print_r($result);
Display above code output like below.
Array
(
[A] => Array
(
[ids] => Array
(
[0] => 1
[1] => 2
[2] => 5
[3] => 6
)
)
[B] => Array
(
[ids] => Array
(
[0] => 3
[1] => 4
)
)
)

How to merge multidimensional arrays where only subarrays are affected? [duplicate]

This question already has answers here:
Merge row data from multiple arrays
(6 answers)
Closed 5 months ago.
here: Transforming array values in elements of a subarray using PHP I asked quite the same, but the arrays were flatter. I tried to adapt the code, but unfortunately without success.
How could I merge following arrays so the second array won't be added after the end of the first array, but each subarray of the first array will receive the correspondent values of subarrays of the second. In other words, I want to merge the subarrays. Here my example:
Array 01:
Array01 (
[0] => Array
(
[0] => 40292633
[1] => 412
)
[1] => Array
(
[0] => 41785603
[1] => 382
)
[2] => Array
(
[0] => 48792980
[1] => 373
)
[3] => Array
(
[0] => 44741143
[1] => 329
))
Array 02:
Array02(
[0] => Array
(
[0] => 3460581
[1] => 1407424B1
[2] => 951753
)
[1] => Array
(
[0] => 3484251
[1] => 1028325B1
[2] => 159357
)
[2] => Array
(
[0] => 3519102
[1] => 0586365A1
[2] => 456654
)
[3] => Array
(
[0] => 3529714
[1] => 1059876A1
[2] => 852258
))
Final array:
finalArray(
[0] => Array
(
[0] => 40292633
[1] => 412
[2] => 3460581
[3] => 1407424B1
[4] => 951753
)
[1] => Array
(
[0] => 41785603
[1] => 382
[2] => 3484251
[3] => 1028325B1
[4] => 159357
)
[2] => Array
(
[0] => 48792980
[1] => 373
[2] => 3519102
[3] => 0586365A1
[4] => 456654
)
[3] => Array
(
[0] => 44741143
[1] => 329
[2] => 3529714
[3] => 1059876A1
[4] => 852258
))
Many thanks in advance!
try this code
function merge_arrays($a1, $a2) {
return array($a1, $a2);
}
$result = array_map("merge_arrays", $a1, $a2);
foreach($result as $nr)
{
$nres[] = array_merge ($nr[0], $nr[1]);
}
Try this:
$result = array();
$keys = array_unique( array_merge(array_keys($arr1), array_keys($arr2)) );
foreach($keys as $key) {
if( !array_key_exists($key, $arr1) ) {
$result[$key] = $arr2;
} else if( !array_key_exists($key, $arr2) ) {
$result[$key] = $arr1;
} else {
$result[$key] = array_merge($arr1[$key], $arr2[$key]);
}
}
It does not assume that both arrays have the same keys.
If you'd like to use array_map, this is an equivalent solution:
$keys = array_unique( array_merge(array_keys($arr1), array_keys($arr2)) );
$result = array_map(function($key) use ($arr1,$arr2) {
if( !array_key_exists($key, $arr1) ) {
return $arr2;
} else if( !array_key_exists($key, $arr2) ) {
return $arr1;
}
return array_merge($arr1[$key], $arr2[$key]);
},
$keys);
If you're sure both arrays have the same keys, you can try this:
$result = array();
foreach($arr1 as $key => $arr1val) {
$result[$key] = array_merge($arr1val, $arr2[$key]);
}

Implode an multidimensional array by keys and values with PHP

I want to join the array keys to a filepath with the value at the end as file itself (the array below is a "filetree")
Array (depth, size and keynames are dynamic):
[0] => bla.tif
[1] => quux.tif
[foo] => Array (
[bar] => Array (
[lorem] => Array (
[1] => ipsum.tif
[2] => doler.tif
)
)
)
[bar] => Array (
[qux] => Array (
[baz] => Array (
[1] => ipsum.tif
[2] => ufo.tif
)
)
)
This result would be fine:
[0] => bla.tif
[1] => quux.tif
[2] => foo/bar/lorem/ipsum.tif
[3] => foo/bar/lorem/doler.tif
[4] => bar/qux/baz/ipsum.tif
[5] => bar/qux/baz/ufo.tif
Maybe there is also a pure PHP solution for that. I tried it with array_map but the results weren't fine enough.
I would use a recursive function to collapse this array. Here's an example of one:
function collapse($path, $collapse, &$result)
{
foreach($collapse AS $key => $value)
{
if(is_array($value))
{
collapse($path . $key . "/", $value, $result);
continue;
}
$result[] = $path . $value;
}
}
And here's how to use:
$result = array();
$toCollapse = /* The multidimentional array */;
collapse("", $toCollapse, $result);
and $result would contain the "imploded" array

how to intersect 2 multidimensional array into third multimensional array

i have 2 arryas and i want them to intersect and store the finding matches into third array with values from first array and second array.
the first array looks like this:
Array
(
[0] => Array
(
[0] => 45
[1] => 10640
[2] => 1041-0567041700116
)
[1] => Array
(
[0] => 46
[1] => 10640
[2] => 1041-0567041700318
)
[2] => Array
(
[0] => 207
[1] => 10645
[2] => 03320103000052
)
and the second array:
Array
(
[0] => Array
(
[0] => 03320103000052
[1] => 0
)
[1] => Array
(
[0] => 10013800805001
[1] => 12
)
[2] => Array
(
[0] => 1090-0360141758201
[1] => 3
)
the out put should be:
Array
(
[0] => Array
(
[0] => 207 =>value from first array
[1] => 10645 =>value from first array
[2] => 03320103000052 =>value from first and second array (this is what i need to compare)
[3] => 0 =>value from second array
)
this is similar to this post
but i have problems to store data into multidimensional array
thanks in forward for any suggestions and help
You can do this with only two foreach loops and one if statement:
$combined = array();
foreach ($array1 as $a) {
foreach ($array2 as $b) {
if ($a[2] == $b[0]) {
$combined[] = array($a[0], $a[1], $a[2], $b[1]);
}
}
}
The following is the test I set up to try this:
<?php
$array1 = array();
$array1[] = array('45', '10640', '1041-0567041700116');
$array1[] = array('46', '10640', '1041-0567041700318');
$array1[] = array('207', '10645', '03320103000052');
$array2 = array();
$array2[] = array('03320103000052', '0');
$array2[] = array('10013800805001', '12');
$array2[] = array('1090-0360141758201', '3');
$combined = array();
foreach ($array1 as $a) {
foreach ($array2 as $b) {
if ($a[2] == $b[0]) {
$combined[] = array($a[0], $a[1], $a[2], $b[1]);
}
}
}
print_r($combined);
?>

Combine arrays to form a multidimensional array

I have three arrays:
$arr1 = Array (
[0] => 1001
[1] => 1007
[2] => 1006);
$arr2 = Array (
[0] => frank
[1] => youi
[2] => nashua);
$arr3 = Array (
[0] => getfrankemail
[1] => getyouiemail
[2] => getnashuaemail);
Is there a way to combine these arrays to get a multidimensional array like this:?
Array (
[0] => Array (
[0] => 1001
[1] => frank
[2] => getfrankemail)
[1] => Array (
[0] => 1007
[1] => youi
[2] => getyouiemail)
[2] => Array (
[0] => 1006
[1] => nashua
[2] => getnashuaemail)
);
edit: what you are really looking for is a php version of the zip method in ruby/python.
For your specific example array_map works nicely:
$result = array_map(null, $arr1, $arr2, $arr3);
Output:
array (
0 =>
array (
0 => 1001,
1 => 'frank',
2 => 'frankemail',
),
1 =>
array (
0 => 1007,
1 => 'youi',
2 => 'youiemail',
),
2 =>
array (
0 => 1006,
1 => 'nashua',
2 => 'nashuaemail',
),
)
Iterate on the first array (looks like those are ids), and you can match the key for each value to indexes in $arr2 and $arr3
$result = array();
foreach ($arr1 as $key => $value) {
$result[] = array($value, $arr2[$key], $arr3[$key]);
}
as #kingkero mentions in his answer, you will get errors if they keys do not exist, which you could check for and ignore any rows where that is the case.
$result = array();
foreach ($arr1 as $key => $value) {
if (!isset($arr2[$key]) || !isset($arr3[$key])) {
continue;
}
$result[] = array($value, $arr2[$key], $arr3[$key]);
}
You could use array_push($aContainer, $arr1); or $aContainer[] = $arr[1]
You can do this with a loop in which you access each of the three arrays with the same key.
$result = array();
$max = count($arr1);
for ($i=0; $i<$max; $i++) {
$result[] = array(
$arr1[$i],
$arr2[$i],
$arr3[$i],
);
}
This could fire an out of bounds exception, since it doesn't check whether or not $arrX[$i] exists. Can you be sure that it does?

Categories