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;
}
Related
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
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've some problem to construct an array.
Array A:
Array
(
[0] => 2015-09-13
[1] => 2015-09-14
[2] => 2015-09-15
[3] => 2015-09-16
[4] => 2015-09-17
[5] => 2015-09-18
[6] => 2015-09-19
)
Array B:
Array
(
[0] => 1
[1] => 8
)
Array C:
Array
(
[0] => Leaves-19
[1] => Shifts-18
[2] => Shifts-18
[3] => Shifts-18
[4] => Shifts-18
[5] => Shifts-18
[6] => Leaves-19
[7] => Leaves-19
[8] => Shifts-12
[9] => Shifts-12
[10] => Shifts-12
[11] => Shifts-12
[12] => Shifts-12
[13] => Leaves-19
)
Desired final output:
Array
(
[0] => 2015-09-13|1|Leaves-19
[1] => 2015-09-14|1|Shifts-18
[2] => 2015-09-15|1|Shifts-18
[3] => 2015-09-16|1|Shifts-18
[4] => 2015-09-17|1|Shifts-18
[5] => 2015-09-18|1|Shifts-18
[6] => 2015-09-19|1|Leaves-19
[7] => 2015-09-13|8|Leaves-19
[8] => 2015-09-14|8|Shifts-12
[9] => 2015-09-15|8|Shifts-12
[10] => 2015-09-16|8|Shifts-12
[11] => 2015-09-17|8|Shifts-12
[12] => 2015-09-18|8|Shifts-12
[13] => 2015-09-19|8|Leaves-19
)
I'm lost in for and foreach.
Here's the logic:
1st parameter is a date and it come's form array B. It is repeat
after 6 entries.
2nd parameter is the user id. It changes after 6 entries and pass to the next id.
3rd parameter is an entry of array B.
Oter informations:
The arrays don't have the same length.
Array A, counts 6 entries.
Array B, counts a random entries.
Array C, is Array A x 2.
I already tried to make a for for my array B and after a foreach in array A, but it wasn't functional.
I do not know where I need to start.
Hope I will have any help or cue.
Thanks a lot.
you can use modulus operator
$OutputArray = Array();
for($i=0; $i < max(count($a1),count($a2),count($a3)); $i++){
array_push($OutputArray, $a1[ $i % count($a1) ] . "|" .
$a2[ $i % count($a2) ] . "|" . $a3[ $i % count($a3) ]);
}
print_r($OutputArray);
you get:
Array
(
[0] => 2015-09-13|1|Leaves-19
[1] => 2015-09-14|8|Shifts-18
[2] => 2015-09-15|1|Shifts-18
[3] => 2015-09-16|8|Shifts-18
[4] => 2015-09-17|1|Shifts-18
[5] => 2015-09-18|8|Shifts-18
[6] => 2015-09-19|1|Leaves-19
[7] => 2015-09-13|8|Leaves-19
[8] => 2015-09-14|1|Shifts-12
[9] => 2015-09-15|8|Shifts-12
[10] => 2015-09-16|1|Shifts-12
[11] => 2015-09-17|8|Shifts-12
[12] => 2015-09-18|1|Shifts-12
[13] => 2015-09-19|8|Leaves-19
)
if you want in order (expected):
$OutputArray = Array();
$max = max(count($a1),count($a2),count($a3));
for($i=0; $i < $max; $i++){
array_push($OutputArray, $a1[$i%count($a1)] . "|" .
$a2[ $i*count($a2) / $max ] . "|" . $a3[$i%count($a3)]);
}
print_r($OutputArray);
you get:
Array
(
[0] => 2015-09-13|1|Leaves-19
[1] => 2015-09-14|1|Shifts-18
[2] => 2015-09-15|1|Shifts-18
[3] => 2015-09-16|1|Shifts-18
[4] => 2015-09-17|1|Shifts-18
[5] => 2015-09-18|1|Shifts-18
[6] => 2015-09-19|1|Leaves-19
[7] => 2015-09-13|8|Leaves-19
[8] => 2015-09-14|8|Shifts-12
[9] => 2015-09-15|8|Shifts-12
[10] => 2015-09-16|8|Shifts-12
[11] => 2015-09-17|8|Shifts-12
[12] => 2015-09-18|8|Shifts-12
[13] => 2015-09-19|8|Leaves-19
)
Try this, it iterates over the biggest array and condition the counting of the minor ones to its desired listing behavior.
<?php
$arrA = ['2015-09-13','2015-09-14','2015-09-15','2015-09-16',
'2015-09-17','2015-09-18','2015-09-19'];
$arrB = [1,8];
$arrC = ['Leaves-19','Shifts-18','Shifts-18','Shifts-18','Shifts-18','Shifts-18',
'Leaves-19','Leaves-19','Shifts-12','Shifts-12','Shifts-12','Shifts-12','Shifts-12',
'Leaves-19'];
$a = $b = 0;
for ($c = 0; $c < count($arrC); $c++) {
$arrC[$c] = $arrA[$a].'|'.$arrB[$b].'|'.$arrC[$c];
$a++;
if ($a == count($arrA)) {
$a = 0;
$b++;
}
};
echo "<pre>";
print_r($arrC);
OUTPUT:
Array
(
[0] => 2015-09-13|1|Leaves-19
[1] => 2015-09-14|1|Shifts-18
[2] => 2015-09-15|1|Shifts-18
[3] => 2015-09-16|1|Shifts-18
[4] => 2015-09-17|1|Shifts-18
[5] => 2015-09-18|1|Shifts-18
[6] => 2015-09-19|1|Leaves-19
[7] => 2015-09-13|8|Leaves-19
[8] => 2015-09-14|8|Shifts-12
[9] => 2015-09-15|8|Shifts-12
[10] => 2015-09-16|8|Shifts-12
[11] => 2015-09-17|8|Shifts-12
[12] => 2015-09-18|8|Shifts-12
[13] => 2015-09-19|8|Leaves-19
)
My version:
$arrayA = array('2015-09-13','2015-09-14','2015-09-15','2015-09-16','2015-09-17','2015-09-18','2015-09-19');
$arrayB = array(1,8);
$arrayC = array('Leaves-19','Leaves-18','Leaves-17','Leaves-16','Leaves-15','Leaves-14','Leaves-13','Leaves-12','Leaves-11','Leaves-10','Leaves-9','Leaves-8','Leaves-7','leaves-6');
$output = array();
$count = 0;
foreach($arrayB as $arrayBElement){
foreach($arrayA as $arrayAElement){
$output[] = $arrayC[$count] . '|' . $arrayAElement . '|' . $arrayBElement;
$count++;
}
}
var_dump($output);
E: removed erroneous count
This stinks of a homework problem so I'm going to answer in pseudocode so hopefully you can apply some hints and come to the solution yourself.
It appears that maybe Array B ([1,8]) is a list of indexes at which to start printing out the next index. Your own statement (2nd parameter is the user id. It changes after 6 entries and pass to the next id.) is not accurate based on your desired final output:
[6] => 2015-09-19|1|Leaves-19
[7] => 2015-09-13|8|Leaves-19
This moves to the next id at index 7 (count 8) not index 6 (count 7, or one after 6 entries).
So My pseudo solution would be this:
Create a new array by repeating Array A until it matches the length of Array C
Append Array B to Array A by finding the element of Array B that is equal to or less than (1+index):
0 => '1', 1 => '1' ... 7 => '8'
Concatenate Array C to every array with index parity (eg. newArray[ 0 ] . ArrayC[ 0 ])
This gives you the final output you desire.
try this code, I have named your 3 arrays as $arrA, $arrB and $arrC
$arrA = Array(
'2015-09-13',
'2015-09-14',
'2015-09-15',
'2015-09-16',
'2015-09-17',
'2015-09-18',
'2015-09-19'
);
$arrB = array(1,8);
$arrC = array(
'Leaves-19',
'Shifts-18',
'Shifts-18',
'Shifts-18',
'Shifts-18',
'Shifts-18',
'Leaves-19',
'Leaves-19',
'Shifts-12',
'Shifts-12',
'Shifts-12',
'Shifts-12',
'Shifts-12',
'Leaves-19'
);
$output = array();
$indx = 0;
foreach($arrB as $b){
foreach($arrA as $a){
$output[] = $a.'|'.$b.'|'.$arrC[$indx];
$indx++;/*new line addded*/
}
//$indx++;/*remove this line from here*/
}
echo "<pre>";
print_r($output);
echo "</pre>";
With different sizes try guess maxlength
//Create arrays
var Adates= ["2015-09-13", "2015-09-14", "2015-09-15"];
var Bnums= ["1", "2", "3"];
var Cnums= ["Leaves-19", "Shifts-18", "Shifts-18"];
//get size
var maxLenght=0;
if(Adates.length>maxLenght) maxLenght=Adates.length;
if(Bnums.length>maxLenght) maxLenght=Bnums.length;
if(Cnums.length>maxLenght) maxLenght=Cnums.length;
//merge and populate
var OutputArray;
for (i = 0; i < maxLenght; i++) {
OutputArray[i]=Adates[i]+"|"+Bnums[i]+"|"+Cnums[i];
}
I have following array with php code
I could not find where I am mistaken
I'm trying to filter some of these array results and delete them. When I try to list them I could not succeded
array (
0 => 'do-update.php',
1 => 'sitemap.xml',
2 => 'sitemap.xml.gz',
3 => 'wp-config.php',
'wp-content' =>
array (
'uploads' =>
array (
2013 =>
array (
'05' =>
array (
0 => 'kabeduvarkad-1024x768.jpg',
1 => 'kabeduvarkad-150x150.jpg',
2 => 'kabeduvarkad-300x225.jpg',
3 => 'kabeduvarkad-940x198.jpg',
4 => 'kabeduvarkad.jpg',
5 => 'kabeduvarkad1-1000x288.jpg',
6 => 'kabeduvarkad1-1024x768.jpg',
7 => 'kabeduvarkad1-150x150.jpg',
8 => 'kabeduvarkad1-300x225.jpg',
9 => 'kabeduvarkad1-400x300.jpg',
10 => 'kabeduvarkad1.jpg',
11 => 'kabeduvarkad2-1000x288.jpg',
12 => 'kabeduvarkad2-1024x768.jpg',
13 => 'kabeduvarkad2-150x150.jpg',
14 => 'kabeduvarkad2-300x225.jpg',
15 => 'kabeduvarkad2-400x300.jpg',
16 => 'kabeduvarkad2.jpg',
),
10 =>
array (
),
),
2014 =>
array (
'02' =>
array (
),
),
),
),
'wp-update' =>
array (
0 => 'wp-update.tar',
1 => 'wp-update.tar.gz',
2 => 'wp-update1.tar',
3 => 'wp-update1.tar.gz',
),
4 => 'wp-update.tar.gz',
)
This is my function
function listArrayRecursive($array_name, $ident = ''){
$result = array();
foreach ($array_name as $k => $v){
if (is_array($v)){
$result[] = listArrayRecursive($v, $ident.'/'.$k);
}else{
$result[] = $ident. '/' . $v . '<br>';
}
}
return $result;
}
I have following result
Array
(
[0] => /do-update.php<br>
[1] => /sitemap.xml<br>
[2] => /sitemap.xml.gz<br>
[3] => /wp-config.php<br>
[4] => Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[0] => /wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg<br>
[1] => /wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg<br>
[2] => /wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg<br>
[3] => /wp-content/uploads/2013/05/kabeduvarkad-940x198.jpg<br>
[4] => /wp-content/uploads/2013/05/kabeduvarkad.jpg<br>
[5] => /wp-content/uploads/2013/05/kabeduvarkad1-1000x288.jpg<br>
[6] => /wp-content/uploads/2013/05/kabeduvarkad1-1024x768.jpg<br>
[7] => /wp-content/uploads/2013/05/kabeduvarkad1-150x150.jpg<br>
[8] => /wp-content/uploads/2013/05/kabeduvarkad1-300x225.jpg<br>
[9] => /wp-content/uploads/2013/05/kabeduvarkad1-400x300.jpg<br>
[10] => /wp-content/uploads/2013/05/kabeduvarkad1.jpg<br>
[11] => /wp-content/uploads/2013/05/kabeduvarkad2-1000x288.jpg<br>
[12] => /wp-content/uploads/2013/05/kabeduvarkad2-1024x768.jpg<br>
[13] => /wp-content/uploads/2013/05/kabeduvarkad2-150x150.jpg<br>
[14] => /wp-content/uploads/2013/05/kabeduvarkad2-300x225.jpg<br>
[15] => /wp-content/uploads/2013/05/kabeduvarkad2-400x300.jpg<br>
[16] => /wp-content/uploads/2013/05/kabeduvarkad2.jpg<br>
)
[1] => Array
(
)
)
[1] => Array
(
[0] => Array
(
)
)
)
)
[5] => Array
(
[0] => /wp-update/wp-update.tar<br>
[1] => /wp-update/wp-update.tar.gz<br>
[2] => /wp-update/wp-update1.tar<br>
[3] => /wp-update/wp-update1.tar.gz<br>
)
[6] => /wp-update.tar.gz<br>
)
Expected Result is
Array
(
[0] => /do-update.php<br>
[1] => /sitemap.xml<br>
[2] => /sitemap.xml.gz<br>
[3] => /wp-config.php<br>
[4] => /wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg<br>
[5] => /wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg<br>
[6] => /wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg<br>
[7] => /wp-content/uploads/2013/05/kabeduvarkad-940x198.jpg<br>
[8] => /wp-content/uploads/2013/05/kabeduvarkad.jpg<br>
[9] => /wp-content/uploads/2013/05/kabeduvarkad1-1000x288.jpg<br>
[10] => /wp-content/uploads/2013/05/kabeduvarkad1-1024x768.jpg<br>
[11] => /wp-content/uploads/2013/05/kabeduvarkad1-150x150.jpg<br>
[12] => /wp-content/uploads/2013/05/kabeduvarkad1-300x225.jpg<br>
[13] => /wp-content/uploads/2013/05/kabeduvarkad1-400x300.jpg<br>
[14] => /wp-content/uploads/2013/05/kabeduvarkad1.jpg<br>
...
[110] => /wp-update/wp-update.tar<br>
[111] => /wp-update/wp-update.tar.gz<br>
[112] => /wp-update/wp-update1.tar<br>
[113] => /wp-update/wp-update1.tar.gz<br>
[114] => /wp-update.tar.gz<br>
)
You can do it like this:
<?php
// Dummy data source
$data = array(
'/do-update.php',
'/sitemap.xml',
'/sitemap.xml.gz',
'/wp-config.php',
array(
array(
array(
'/wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg',
'/wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg',
'/wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg<br>'
)
)
)
);
// Helper function
function getFiles($data, &$fileList) {
foreach ($data as $dataItem) {
if (is_array($dataItem))
getFiles($dataItem, $fileList);
else
$fileList[] = $dataItem;
}
}
// Debug
echo "<b>Orignal Array</b>";
var_dump($data);
echo "<hr>";
// Helper function usage
echo "<b>Parsed Array</b>";
$fileList = array();
getFiles($data, $fileList);
var_dump($fileList);
?>
Output:
Ok use this function its working
$all=array();
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach($it as $v) {
$all[]=$v;
}
print_r($all);
With $var[] = you basically say that you want to add a new Element to $var with an incremented key.
Your Recursive frunction returns an array.
So this array is assigned as a new element in your array.
But what you want is a flat array.
Instead of adding an array to your array like this:
if (is_array($v)){
$result[] = listArrayRecursive($v, $ident.'/'.$k);
Merge the existing arrays like this:
if (is_array($v)){
$tmpResult = listArrayRecursive($v, $ident.'/'.$k);
$result = array_merge($result, $tmpResult);
You can see a working example here.
I've been having a little trouble trying to flatten arrays in a specific way.
Here is a print_r view of the array I want to flatten:
Array
(
[1] => Array
(
[8] => 1
[9] => 2
[10] => Array
(
[15] => Array
(
[22] => 1
)
[21] => 2
)
[11] => Array
(
[16] => Array
(
[23] => 1
)
)
)
[2] => Array
(
[12] => 1
)
[3] => Array
(
[13] => 1
)
[4] => Array
(
[14] => 1
)
[5] => 5
[6] => 6
[7] => 7
)
What I'm attempting to create is an array which keeps the above indexes, but the value is equal to it's position in the array, much like the original index (starting from zero).
Here is the desired result:
Array
(
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
[8] => 1
[9] => 2
[10] => 3
[11] => 4
[12] => 1
[13] => 1
[14] => 1
[15] => 1
[16] => 1
[21] => 2
[22] => 2
[23] => 1
)
Knowingly, 17 to 20 are missing.
My function is as follows:
function array_flatten ($array) {
$result = array ();
$count = 1;
while ($index = current($array)) {
$result[key($array)] = $count;
if (is_array($index)) {
$result = array_merge(array_flatten($index), $result);
}
next($array);
$count++;
}
return $result;
}
The line $result = array_merge(array_flatten($index), $result); appears to be the problems. It returns:
Array
(
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
)
However, if I run var_dump(array_flatten($index)); on the same line, it returns all the arrays I wish to merge to the $result variable.
array
22 => int 1
array
15 => int 1
21 => int 2
array
23 => int 1
array
16 => int 1
array
8 => int 1
9 => int 2
10 => int 3
11 => int 4
array
12 => int 1
array
13 => int 1
array
14 => int 1
It seems that that array_merge doesn't actually merge these arrays.
Is there something I am doing wrong? Any words of guidance are very much appreciated. Thank you.
Update
Solved!
The function which does the required is as follows:
function array_flatten($array, &$result = array()) {
$count = 1;
foreach($array as $index => $value) {
$result[$index] = $count;
if(is_array($value)) {
array_flatten($value, $result);
}
$count++;
}
return $result;
}
function flatten_array($array, &$result) {
foreach($array as $key => $value) {
if(is_array($value)) {
flatten_array($value, $result);
} else {
$result[$key] = $value;
}
}
}
To use this, check the sample code below:
$flattened = array();
$test = array(
1 => 1
, 3 => 2
, array(2 => 4, 4 => 6)
, 5 => 3
, array(7 => 9, 8 => 7, 9 => 5)
);
flatten_array($test, $flattened);
// Now $flattened contains the flattened array
After you have clarified your question, I was a bit surprised that you accepted an answer that does not return the data you expected. (I've now seen you added your solution to your question.)
What I did: I took the function from #Arjan as a base, and run it on the questions input data and compared it with the questions expected data. Then I worked on it a bit. This is what I came up with (as a result):
# COMP EXPECTED . ACTUAL
#00: == Array . Array
#01: == ( . (
#02: == [1] => 1 . [1] => 1
#03: == [2] => 2 . [2] => 2
#04: == [3] => 3 . [3] => 3
#05: == [4] => 4 . [4] => 4
#06: == [5] => 5 . [5] => 5
#07: == [6] => 6 . [6] => 6
#08: == [7] => 7 . [7] => 7
#09: == [8] => 1 . [8] => 1
#10: == [9] => 2 . [9] => 2
#11: == [10] => 3 . [10] => 3
#12: == [11] => 4 . [11] => 4
#13: == [12] => 1 . [12] => 1
#14: == [13] => 1 . [13] => 1
#15: == [14] => 1 . [14] => 1
#16: == [15] => 1 . [15] => 1
#17: == [16] => 1 . [16] => 1
#18: == [21] => 2 . [21] => 2
#19: != [22] => 2 . [22] => 1
#20: == [23] => 1 . [23] => 1
#21: == ) . )
#22: == .
It looks like that your expected data has a mistake for position 22.
This is the modified function (Demo):
function flatten_array($array, &$result = null) {
$r = null === $result;
$i = 0;
foreach($array as $key => $value) {
$i++;
if(is_array($value)) {
$result[$key] = $i;
flatten_array($value, $result);
} else {
$result[$key] = $value;
}
}
if ($r) {
ksort($result);
return $result;
}
}
$actual = flatten_array($input);