Merge Subarray (PHP 5.3.3) [duplicate] - php

This question already has answers here:
Merge all sub arrays into one [duplicate]
(3 answers)
Closed 2 years ago.
I want to merge all sub arrays of an array in an older project that requires >=5.3.3.
The array mentioned above looks like this:
$array = Array (
Array
(
[0] => 26
[1] => 644
)
Array
(
[0] => 20
[1] => 26
[2] => 644
)
Array
(
[0] => 26
)
Array
(
[0] => 47
)
Array
(
[0] => 47
[1] => 3
[2] => 18
)
Array
(
[0] => 26
[1] => 18
)
Array
(
[0] => 26
[1] => 644
[2] => 24
[3] => 198
[4] => 8
[5] => 6
[6] => 41
[7] => 31
)
Array
(
[0] => 26
[1] => 644
[2] => 24
[3] => 198
[4] => 12
[5] => 25
[6] => 41
[7] => 31
)
Array
(
[0] => 198
)
Array
(
[0] => 198
)
Array
(
[0] => 899
))
Now, I'm wondering how I could merge all of those Subrrays into one that looks like this:
Array
(
[1] => 26
[2] => 644
[3] => 20
[4] => 26
[5] => 644
[6] => 26
[7] => 47
[8] => 47
[9] => 3
[10] => 18
[11] => 26
[12] => 18
[13] => 26
[14] => 644
[15] => 24
[16] => 198
[17] => 8
[18] => 6
[19] => 41
[20] => 31
[21] => 26
[22] => 644
[23] => 24
[24] => 198
[25] => 12
[26] => 25
[27] => 41
[28] => 31
[29] => 198
[30] => 198
[31] => 899
)
I know how this could work on a more up to date PHP version. So far on this older version I've tried the following:
print_r(array_merge($array, $emptyArray));
But I get the exact same Array returned.
I also tried something like this:
$result_arr = array();
foreach ($array as $sub_arr) $result_arr = array_merge($result_arr, $sub_arr);
$result_arr = array_unique($result_arr);
print_r($result_arr);
Which tells me my second argument is not an array?
I'm a bit confused and hope someone can shed some light on this issue.

this function, I think, will do it well.
function array_flatten(array $array)
{
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, array_flatten($value));
} else {
$result = array_merge($result, array($key => $value));
}
}
return $result;
}
All you have to do is send your array as a parameter of the function, and display the result.
$arrayToFlatt = Array(
Array(26, 644),
Array(20, 26, 644),
Array(26),
Array(47),
Array(47, 3, 18),
Array(26, 18),
Array(26, 644, 24, 198, 8, 6, 41, 31),
Array(26, 644, 24, 198, 12, 25, 41, 31),
Array(198),
Array(198),
Array(899)
);
echo '<pre>';
print_r(array_flatten($arrayToFlatt));
echo '</pre>';
Working since PHP 4

You could use array_walk_recursive(), which will visit each leaf node of the input array, then add this value to an output array...
$output = array();
array_walk_recursive($array, function ($data) use (&$output) {
$output[] = $data;
});
print_r($output);

Your foreach approach is fine, and works for me. But I don't see why you are filtering with array_unique.
<?php
$data =
[
[1,3,5,7],
[2,4,6,8],
[1,2,4,5]
];
$output = [];
foreach ($data as $sub) {
$output = array_merge($output, $sub);
}
var_export($output);
Output:
array (
0 => 1,
1 => 3,
2 => 5,
3 => 7,
4 => 2,
5 => 4,
6 => 6,
7 => 8,
8 => 1,
9 => 2,
10 => 4,
11 => 5,
)
To flatten with the splat (Php 5.6):
$output = array_merge(...$data);
For pre-splat versions:
$output = call_user_func_array('array_merge', $data);
Or array_reduce:
$output = array_reduce($data, 'array_merge', []);
If in doubt foreach:
$output = [];
foreach($data as $v)
foreach($v as $n)
$output[] = $n;
All the above result in the same zero indexed array.

Related

Concatenate value elements of two or more different arrays in PHP

I originally have this array
Array
(
[0] => Amministrativo ^25^9,11,2,10,18,4,7,^17,13,^0.75^0^0.25
[1] => Logico deduttive^7^^21,^0.75^0^-0.25
[2] => Situazionali^8^^20,^0.75^0^0.375
)
Using the function explode and array_diff i can get to this
Array
(
[0] => Amministrativo
[1] => 25
[2] => 9,11,2,10,18,4,7,
[3] => 17,13,
[4] => 0.75
[5] => 0
[6] => 0.25
)
Array
(
[0] => Logico deduttive
[1] => 7
[2] =>
[3] => 21,
[4] => 0.75
[5] => 0
[6] => -0.25
)
Array
(
[0] => Situazionali
[1] => 8
[2] =>
[3] => 20,
[4] => 0.75
[5] => 0
[6] => 0.375
)
but I would like to concatenate the elements of each array to get a unique array. I think I need to use the array_map function but I don't know how. This below is the result I would like to achieve
Array (
[0] => Amministrativo Logico deduttive Situazionali
[1] => 25 7 8
[2] => 9,11,2,10,18,4,7,
[3] => 17,13,21,20,
[4] => 0.75 0.75 0.75
[5] => 0 0 0
[6] => 0.25 -0.25 0.375
)
tnks
EDIT:
I tried the code that is here and it is fine.
But now I realized that there is also the problem that the arrays can be in variable number 1, 2, 3 or more and I can't know it before, I should adapt this code
$result = array_map(function ($item1, $item2,$item3) {
return "$item1 $item2 $item3";
}, $test[0], $test[1],$test[2]);
The lines of the original array are split with explode. The result is a two-dimensional array. This is transposed (swapping rows and columns). Then the lines are reassembled with implode.
function array_transpose(array $a) {
$r = array();
foreach($a as $keyRow => $subArr) {
foreach($subArr as $keyCol => $value) $r[$keyCol][$keyRow] = $value;
}
return $r;
}
$arr = [
'Amministrativo ^25^9,11,2,10,18,4,7,^17,13,^0.75^0^0.25',
'Logico deduttive^7^^21,^0.75^0^-0.25',
'Situazionali^8^^20,^0.75^0^0.375',
];
foreach($arr as $i => $row){
$arr[$i] = explode('^',$row);
}
$arr = array_transpose($arr);
foreach($arr as $i => $row){
$arr[$i] = implode(' ',$row);
}
var_export($arr);
Demo: https://3v4l.org/IdsDh

remove duplicates in multi array with different keys

i know that this question is answered more times here. but i can't figure out how to do this
i have an array with arrays in IT
Array
(
[11] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 37
[4] => 41
)
[4] => Array
(
[0] => 67
[1] => 74
[2] => 80
[3] => 82
[4] => 94
[5] => 95
[6] => 100
)
[15] => Array
(
[0] => 86
[1] => 100
[2] => 94
)
)
as you see in 4 and 15 keys we have duplicates (94 and 100)
i want to remove all this duplicates except one
so in final to have
Array
(
[11] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 37
[4] => 41
)
[4] => Array
(
[0] => 67
[1] => 74
[2] => 80
[3] => 82
[4] => 94
[5] => 95
[6] => 100
)
[15] => Array
(
[0] => 86
)
)
Big thanks
Loop over your outer array, inside loop over your inner arrays.
Check if the current value is already contained in a helper array, if so unset the array element (using the outer and inner level key.)
Then add the current value to the helper array, so that it can be recognized as a duplicate value when it occurs the next time.
$data = [
11 => [3, 4, 5, 37, 41],
4 => [67, 74, 80, 82, 94, 95, 100],
15 => [86, 100, 94]
];
$helperArray = [];
foreach($data as $dkey => $item) {
foreach($item as $ikey => $value) {
if(in_array($value, $helperArray)) {
unset($data[$dkey][$ikey]);
}
$helperArray[] = $value;
}
}
print_r($data);
Here is the most un-elegant solution but it will always do the trick...
<?php
/* Create the array */
$array[11][] = 3;
$array[11][] = 4;
$array[11][] = 5;
$array[11][] = 37;
$array[11][] = 41;
$array[4][] = 67;
$array[4][] = 74;
$array[4][] = 80;
$array[4][] = 82;
$array[4][] = 94;
$array[4][] = 95;
$array[4][] = 100;
$array[15][] = 86;
$array[15][] = 100;
$array[15][] = 94;
/* print the complete array : */
echo '<pre>';
print_r($array);
echo '<pre>';
/* use a simple array to store values as you loop */
$A = [];
foreach($array as $key => $ar){
foreach($ar as $k => $a){
/* if the value already appeared somewhere... remove it from this and next locations...*/
if(in_array($a,$A)){
unset($array[$key][$k]);
}
$A[] = $a;
}
}
/* Print our cleaned array. */
print_r($array);
will return:
Array
(
[11] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 37
[4] => 41
)
[4] => Array
(
[0] => 67
[1] => 74
[2] => 80
[3] => 82
[4] => 94
[5] => 95
[6] => 100
)
[15] => Array
(
[0] => 86
[1] => 100
[2] => 94
)
)
Array
(
[11] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 37
[4] => 41
)
[4] => Array
(
[0] => 67
[1] => 74
[2] => 80
[3] => 82
[4] => 94
[5] => 95
[6] => 100
)
[15] => Array
(
[0] => 86
)
)
Next time , please share your code, as bad as it can be or whatever it shows your way and that you have tried solving this for real...
I made the data a bit more complex, so that the duplicates are not only in the array elements with the keys 4 and 15.
$data = [
11 => [ 3, 4, 94, 5, 80, 37, 41 ], // will be: 3, 4, 94, 5, 80, 37, 41
4 => [ 67, 74, 80, 82, 95, 100 ], // will be: 67, 74, 82, 95, 100 (80 > key 11)
7 => [ 2, 4, 77, 81, 13, 100 ], // will be: 2, 77, 81, 13 (4 > key 11, 100 > key 4)
15 => [ 86, 100, 94, 13 ] // will be: 86 (100 > key 7, 94 > key 11, 13 > key 7)
];
$keys = array_reverse(array_keys($data));
for ($i = 0; $i < count($keys); $i++) {
$keyI = $keys[$i];
for ($j = $i + 1; $j < count($keys); $j++) {
$keyJ = $keys[$j];
$diff = array_diff($data[$keyI], $data[$keyJ]);
$data[$keyI] = $diff;
}
}
print_r($data);

How to merge mutiple array in a single array

As i searched alot but didn't get perfect solution of merging array in one single array.
These arrays are Dynamic (may be increase in future- would 50+). So for that we have to use count() or for loops to fetch and then merge.
Here's my code which i'm trying to resolve over core level. Please anyone tell me how may receive all values in Single array.
Array(
[0] => Array
(
[0] => 123
[1] => 108
[2] => 58
[3] => 23
)
[1] => Array
(
[0] => 93
[1] => 94
[2] => 95
[3] => 172
[4] => 30
)
[2] => Array
(
[0] => 109
[1] => 81
[2] => 79
[3] => 155 )
)`
My expectation of result is: (which i'm unable to get)
Array
(
[0] => 123
[1] => 108
[2] => 58
[3] => 23
[4] => 93
[5] => 94
[6] => 95
[7] => 172
[8] => 30
[9] => 109
[10] => 81
[11] => 79
[12] => 155
)
Use array_merge with splat operator,
$result = array_merge(...$arr);
array_merge — Merge one or more arrays
Splat operator - Since PHP 5.6, you can use splat operator (...) to create simpler variadic functions (functions that take an undefined number of arguments).
Demo
Output:-
Array
(
[0] => 123
[1] => 108
[2] => 58
[3] => 23
[4] => 93
[5] => 94
[6] => 95
[7] => 172
[8] => 30
[9] => 109
[10] => 81
[11] => 79
[12] => 155
)
using array_merge()
$a[0] = [1,2,3];
$a[1] = [4,5,6];
$a[2] = [7,8,9];
$newArr = [];
$newArr = array_merge($a[0], $a[1], $a[2]);
print_r($newArr);
assuming that your array will grow, you can use foreach like this :
$a[0] = [1,2,3];
$a[1] = [4,5,6];
$a[2] = [7,8,9];
$newArr = [];
foreach($a as $index){
foreach($index as $item){
array_push($newArr, $item);
}
}
As i used this technique. I got my answer in just a simple way. of array_flatten
print_r(array_flatten($overall_arr));
function array_flatten($array) {
$return = array();
foreach ($array as $key => $value) {
if (is_array($value)){
$return = array_merge($return, array_flatten($value));
} else {
$return[$key] = $value;
}
}
return $return;
}

PHP return duplicate values from an Array

i have array which has multiple duplicate values. now i want to get those duplicate values.
$roQ = Array
(
[0] => 2123
[1] => 2094
[2] => 2105
[3] => 2160
[4] => 2143
[5] => 2148
[6] => 2154
[7] => 2155
[8] => 2145
[9] => 2123
[10] => 2149
[11] => 2143
[12] => 2145
)
i tried following code which is not working. it returns incorrect result.
how to get the all duplicate values in new array.
$c = array_count_values($roQ);
$val = array_search(max($c), $c);
$azq[] = $val;
Try with -
$arr = array(1, 4, 6, 1, 8, 9, 4, 6);
$unique = array_unique($arr);
$duplicates = array_diff_assoc($arr, $unique);
var_dump($duplicates);
$roQ = Array
(
[0] => 2123
[1] => 2094
[2] => 2105
[3] => 2160
[4] => 2143
[5] => 2148
[6] => 2154
[7] => 2155
[8] => 2145
[9] => 2123
[10] => 2149
[11] => 2143
[12] => 2145
)
for($i=0; $i<= count($roQ), $i++){
if(!in_array($roQ[$i],$arr){
$unique_value[] = $roQ[$i];
}else {
$dublicate_array[]=$roQ[$i]
}
array_push($arr, $req[$i]);
}
Try this code it displays the duplicate elements in an array.
$array = array(
0 => 2123,
1 => 2094,
2 => 2105,
3 => 2160,
4 => 2143,
5 => 2148,
6 => 2154,
7 => 2155,
8 => 2145,
9 => 2123,
10 => 2149,
11 => 2143,
12 => 2145
);
$arrayNew = array_count_values($array);
$finalArr = array ();
$i=0;
foreach ($arrayNew as $key => $Array_new){
if($Array_new>1){
$finalArr[$i] = $key;
$i++;
}
}
echo "<pre>";
print_r($finalArr);
exit;
}

Changing array key value in PHP

I have an array like below,
[test] => Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 13
[4] => 32
[5] => 51
)
i need to change this array into like below,
[test] => Array
(
[2] => 1
[4] => 3
[6] => 5
[8] => 13
[10] => 32
[12] => 51
)
i need to change the key value. How can i do this?.
$newArray = array_combine(
range(2,count($originalArray)*2,2),
array_values($originalArray)
);
The array_values() function returns an array containing all the values of an array and also it reset all the keys. you can do it as
$arr = array(0 => 1, 1 => 3, 2 => 5, 3 => 13, 4 => 32, 5 => 51);
$count = 1;
$tempArr = array();
foreach ($arr as $key => $val) {
$tempArr[$count * 2] = $val;
$count++;
}
var_dump($tempArr);exit;
Try this code at your side.

Categories