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;
}
Related
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.
I have tried to split an Array Value with the same first character into a key and last character into a value. Here is an example.
$array :
(
[0] => a6
[1] => a7
[2] => b2
[3] => b7
[4] => c7
[5] => c6
[6] => d6
[7] => d7
[8] => e3
[9] => e0
)
I want to make a new $array like this :
( [a] => 67
[b] => 27
[c] => 76
[d] => 67
[e] => 30 )
So far I have tried this function code :
function array_split($array, $pieces=2)
{
if ($pieces < 2)
return array($array);
$newCount = ceil(count($array)/$pieces);
$a = array_slice($array, 0, $newCount);
$b = array_split(array_slice($array, $newCount), $pieces-1);
return array_merge(array($a),$b);
}
$arrayz = array_split($array, 5);
but this code has the following results :
$arrayz :
(
[0] => Array
(
[0] => a6
[1] => a7
)
[1] => Array
(
[0] => b2
[1] => b7
)
[2] => Array
(
[0] => c7
[1] => c6
)
[3] => Array
(
[0] => d6
[1] => d7
)
[4] => Array
(
[0] => e3
[1] => e0
)
)
What should the function be to get the result?
Easiest solution:
$res = [];
foreach ($array as $v) {
$res[$v[0]] = isset($res[$v[0]]) ? $res[$v[0]].$v[1] : $v[1];
}
print_r($res);
Where $array is ['a6','a7','b2', 'b7','c7','c6','d6','d7','e3','e0']
Strings in PHP being a character array we can process string as a character array.
<?php
$a=array('a6','a7','b2','b7','c7','c6','d6','d7','e3','e0');
$x=array();
foreach($a as $val)
{
$x[$val[0]].=$val[1];
}
echo '<pre>'; print_r($x);
?>
Here is the code for storing unique values to a variable.
$filtermodel = array_unique($list2);
Output:
Array
(
[0] => 15592
[1] => 182
[4] => 14208
[7] => 183
[11] => 184,185,186
[15] => 174
[16] => 177
[23] => 184,186
[24] => 184,185,186,187
[29] => 179
[38] => 188
[41] => 174,176,184,185,186,187
)
Above array key[11] contains 184,185,186 and key[23][24] contains same as key[11]. How to remove those duplicates in array? Any help? Thanks in advance.
You can go with a foreach over each element in array and explode by comma , then do a unique on the resulted list of strings
Here is a Demo
If we suppose that the '182,186' for example is an array of integers and got right what you are trying to achieve the below works :
If not then please consider pointing out better what are you trying to achieve
$mArray = array(
15592,
182,
14208,
183,
array(184,185,186),
174,
177,
array(184,186),
array(184,185,186,187),
179,
188,
array(174,176,184,185,186,187),
);
$mFinalArray = [];
foreach($mArray as $currentObject)
if(!is_array($currentObject) && !in_array($currentObject, $mFinalArray))
$mFinalArray[] = $currentObject;
else if(is_array($currentObject))
foreach($currentObject as $currentObjectInside)
if(!in_array($currentObjectInside, $mFinalArray))
$mFinalArray[] = $currentObjectInside;
sort($mFinalArray);
print_r($mFinalArray);
Output :
Array
(
[0] => 174
[1] => 176
[2] => 177
[3] => 179
[4] => 182
[5] => 183
[6] => 184
[7] => 185
[8] => 186
[9] => 187
[10] => 188
[11] => 14208
[12] => 15592
)
You can use array_walk with passing by reference, using the third parameter (userdata) as dictionary for used values:
array_walk($array, function(&$item, $_, &$dict) {
$values = explode(',', $item);
$item = implode(',', array_filter($values, function ($value) use (&$dict) {
// If value already in the dictionary, just skip it.
if (isset($dict[$value])) {
return false;
}
// Add value to the dictionary and not filter it out.
return $dict[$value] = true;
}));
}, []);
Here is working demo.
Try using a foreach loop and for each row contains ',' symbol try using explode.
You can also create an array just for holding the existing values for later check if they exist.
Simple example-
$unique_values = [];
$all_values = array (
[0] => 15592
[1] => 182
[4] => 14208
[7] => 183
[11] => 184,185,186
[15] => 174
[16] => 177
[23] => 184,186
[24] => 184,185,186,187
[29] => 179
[38] => 188
[41] => 174,176,184,185,186,187
);
foreach( $all_values as $av ) {
if( strpos( $av, ',' ) !== false ) {
$av = explode( ',', $av );
foreach( $av as $a ) {
array_push( $unique_values, $a );
array_unique( $unique_values );
$result = !empty( array_intersect( $unique_values, $a ) );
}
}
}
I didn't test this code but i made it for explaining the logic.
I have this two arrays
$views[] = $id;
$pid[] = $page_id;
which prints
Array
(
[0] => 9
[1] => 12
[2] => 13
[3] => 14
[4] => 15
)
Array
(
[0] => 174
[1] => 221
[2] => 174
[3] => 174
[4] => 174
)
now i want to create new array from this result like(first will be the key and second be the value)
Array
(
[9] => 174
[12] => 221
[13] => 174
[14] => 174
[15] => 174
)
I have tired array_push function but didnt work for me.
You can use array_combine:
Creates an array by using one array for keys and another for its
values
ie:
$newarr = array_combine($array1, $array2); //$array1: key, $array2: value
$result = array();
for($i=0; $i<sizeof($array1); $i++)
$result[$array1[$i]] = $array2[i];
I need to find common values in multiple arrays. Number of arrays may be infinite.
Example (output from print_r)
Array1
(
[0] => 118
[1] => 802
[2] => 800
)
Array2
(
[0] => 765
[1] => 801
)
Array3
(
[0] => 765
[1] => 794
[2] => 793
[3] => 792
[4] => 791
[5] => 799
[6] => 801
[7] => 802
[8] => 800
)
now, I need to find the values that are common on all 3 (or more if available) of them.... how do I do that?
Thanx
array_intersect()
$intersect = array_intersect($array1,$array2,$array3);
If you don't know how many arrays you have, then build up an array of arrays and user call_user_func_array()
$list = array();
$list[] = $array1;
$list[] = $array2;
$list[] = $array3;
$intersect = call_user_func_array('array_intersect',$list);