Pair all elements between them without duplicates - php

At this point my script is working but not fully. I managed to pair elements without duplicates, but i cant seem to find any way to repeat the loop so i can get all the possible results. From 20 possible results i get only 16 to 19 results. Any help would be much appreciated.
Here's the long code + output
$studentList = array
(
array('up1','up2','up3','up4','up5','up6', 'up7', 'up8' , 'up9' , 'up10'),
array('up1','up2','up3','up4','up5','up6', 'up7', 'up8' , 'up9' , 'up10'),
array('up1','up2','up3','up4','up5','up6', 'up7', 'up8' , 'up9' , 'up10'),
);
//count how many times the user wants to pair students up
$AC = count($studentList);
//Take away one from the count due to the first aray used for setting up the pairs
$AC--;
//count how may users need to be paired
$c = count($studentList[0]);
$totalCount = $AC * $c;
echo $totalCount."<= total count<br>";
for ($b = 1; $b <= $AC; $b++)
{
shuffle($studentList[$b]);
print_R ($studentList[$b]);
echo"<br>";
}
$z = 0;
$r = 1;
$flagReset = 0;
//this will look to make sure the results are random
for ($i = 0; $i < $totalCount; $i++)
{
if ($studentList[0][$z] == $studentList[$r][$z]){}
else
{
$randomArray[$i] = $studentList[0][$z] ."/".$studentList[$r][$z];
}
//echo $z."<= z count|";
if ($i == 0)
{
echo "i am the first $z / $c / $r <br>";
}
if ($z == $c - 1 )
{
if ($i < $totalCount -1 )
{
$r++;
$z= 0;
$flagReset = 1;
}
else
{
$flagReset = 1;
}
}
if ($flagReset == 1)
{
$flagReset = 0;
}
else
{
$z++;
}
if ($i == 19)
{
echo "i am the last $z / $c / $r <br>";
}
array_unique($randomArray, SORT_REGULAR);
$rand = count($randomArray);
}
echo "<br>";
array_unique($randomArray, SORT_REGULAR);
$rand = count($randomArray);
print_r($randomArray);
print $rand;
Output:
20<= total count
Array ( [0] => up8 [1] => up9 [2] => up2 [3] => up4 [4] => up5
[5] => up6 [6] => up3 [7] => up10 [8] => up1 [9] => up7 )
Array ( [0] => up4 [1] => up5 [2] => up8 [3] => up7 [4] => up6 [5] => up1
[6] => up2 [7] => up9 [8] => up10 [9] => up3 )
i am the first 0 / 10 / 1
i am the last 9 / 10 / 2
Array ( [0] => up1/up8 [1] => up2/up9 [2] => up3/up2 [6] => up7/up3
[7] => up8/up10 [8] => up9/up1 [9] => up10/up7 [10] => up1/up4
[11] => up2/up5 [12] => up3/up8 [13] => up4/up7 [14] => up5/up6
[15] => up6/up1 [16] => up7/up2 [17] => up8/up9 [18] => up9/up10
[19] => up10/up3 ) 17

Not really anymore a temporary answer, just an answer
From what I've understood, you're trying to find all the possible combinations from two shuffled arrays of the array $studentlist.
I've tried reading over and over the code you've written, but I'm not really understanding why you're using so many for loops and flag.
What I've tried to do is this:
<?php
// stackoverflow test area
$studentList = array
(
array('up1','up2','up3','up4','up5','up6', 'up7', 'up8' , 'up9' , 'up10'),
array('up1','up2','up3','up4','up5','up6', 'up7', 'up8' , 'up9' , 'up10'),
array('up1','up2','up3','up4','up5','up6', 'up7', 'up8' , 'up9' , 'up10')
);
//count how many times the user wants to pair students up
$AC = count($studentList);
//Take away one from the count due to the first aray used for setting up the pairs
$AC--;
//count how may users need to be paired
$c = count($studentList[0]);
$totalCount = $AC * $c;
echo $totalCount."<= total count<br>";
for ($b = 1; $b <= $AC; $b++)
{
shuffle($studentList[$b]);
print_R ($studentList[$b]);
echo"<br>";
}
$randomArray = array();
//this will look to make sure the results are random
foreach ($studentList[0] as $value) {
foreach ($studentList[1] as $second_value) {
if ($value !== $second_value) {
if (!in_array("{$value}/{$second_value}",$randomArray) and !in_array("{$second_value}/{$value}",$randomArray)) {
$randomArray[] = "{$value}/{$second_value}";
}
}
}
}
array_unique($randomArray, SORT_REGULAR);
$rand = count($randomArray);
print_r($randomArray);
print $rand;
?>
The big deal is here:
foreach ($studentList[0] as $value) {
foreach ($studentList[1] as $second_value) {
if ($value !== $second_value) {
if (!in_array("{$value}/{$second_value}",$randomArray) and !in_array("{$second_value}/{$value}",$randomArray)) {
$randomArray[] = "{$value}/{$second_value}";
}
}
}
}
I'm not understing why you're doing it with a regular for loop.
The goal is basicly finding all the possible combinations, right? so why not looping through both the arrays and just check if:
the values are identical ( ignore in this case ).
the values are different.
In the second case, there are two possibilities:
the value x1/x2 or x2/x1 already exists in the array (ignore).
the value x1/x2 or x2/x1 doesn't yet exist. Push it.
And the result is this:
20<= total count
Array ( [0] => up4 [1] => up10 [2] => up9 [3] => up6 [4] => up5 [5] => up7 [6] => up3 [7] => up8 [8] => up2 [9] => up1 )
Array ( [0] => up10 [1] => up8 [2] => up6 [3] => up5 [4] => up7 [5] => up4 [6] => up1 [7] => up2 [8] => up3 [9] => up9 )
Array ( [0] => up1/up4 [1] => up1/up10 [2] => up1/up9 [3] => up1/up6 [4] => up1/up5 [5] => up1/up7 [6] => up1/up3 [7] => up1/up8 [8] => up1/up2 [9] => up2/up4 [10] => up2/up10 [11] => up2/up9 [12] => up2/up6 [13] => up2/up5 [14] => up2/up7 [15] => up2/up3 [16] => up2/up8 [17] => up3/up4 [18] => up3/up10 [19] => up3/up9 [20] => up3/up6 [21] => up3/up5 [22] => up3/up7 [23] => up3/up8 [24] => up4/up10 [25] => up4/up9 [26] => up4/up6 [27] => up4/up5 [28] => up4/up7 [29] => up4/up8 [30] => up5/up10 [31] => up5/up9 [32] => up5/up6 [33] => up5/up7 [34] => up5/up8 [35] => up6/up10 [36] => up6/up9 [37] => up6/up7 [38] => up6/up8 [39] => up7/up10 [40] => up7/up9 [41] => up7/up8 [42] => up8/up10 [43] => up8/up9 [44] => up9/up10 ) 45
Am I going wrong somewhere or is this what you're looking for?

Related

find the minimum and maximum value in array group php

Array
(
[0] => Array
(
[package] => LTE_15AGB
[value] => Array
(
[0] => 52690
[1] => 24700
[2] => 43972
[3] => 506417
[4] => 488125
[5] => 935918
[6] => 1322816
[7] => 1189040
[8] => 2805279
[9] => 2764825
[10] => 1688294
[11] => 1228812
[12] => 2232345
[13] => 3356143
[14] => 1193213
[15] => 167589
[16] => 1373104
[17] => 691411
[18] => 1398647
[19] => 5
)
)
[1] => Array
(
[package] => LTE_15AGB_NT
[value] => Array
(
[0] => 953370
[1] => 151168
[2] => 37605
[3] => 428769
[4] => 755222
[5] => 1146719
[6] => 494289
[7] => 889002
[8] => 307200
[9] => 127972
[10] => 2764815
[11] => 1426224
[12] => 262669
[13] => 648757
[14] => 1485
[15] => 1202
[16] => 998
[17] => 1
)
)
)
This is what I have tried:
$tmp = array();
foreach($arrayName as $arg){
$tmp[$arg['package']][] = $arg['value'];
}
$output = array();
foreach($tmp as $type => $labels){
$output[] = array( 'package' => $type, 'value' => $labels, );
}
print_r(($output))
Try this:
foreach ($your_array as $subarr) {
echo $subarr[package]." minimum = ";
echo min($subarr[value])." and maximum = ";
echo max($subarr[value])."<br>";
}
this will output each package name together with the minimum and maximum values.
Simply use the min() and max() functions.
Your code:
foreach(array_column($array, 'value') as $key => $values){
echo PHP_EOL . 'SubArray '. $key .' min = '. min($values) . ' and max value = '. max($values);
}
output is:
SubArray 0 min = 5 and max value = 3356143
SubArray 1 min = 1 and max value = 2764815
References:
PHP min() manual
PHP max()) manual
PHP array_column
manual
Live demo: https://eval.in/941702
#Arebhy Sri, You should search about array in php, It's like basic problem.
$finalArray = [];
foreach ($mainArr as $subArr){
$array = $subArr['value'];
sort($array);
$tempArray['minimum'] = reset($array);
$tempArray['maximum'] = end($array);
$tempArray['package'] = $subArr['package'];
$finalArray[] = $tempArray;
}
$finalArray //you can use
I am using simple foreach and two functions of array reset() and end().
reset(): Returns the value of the first array element, or FALSE if the array is empty.
end(): Returns the value of the last element or FALSE for empty array.

Build an array from 3 arrays with PHP

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];
}

Sum numbers in array

I have an array of numbers (zipcode-areas), like:
Array (
[0] => 34
[1] => 35
[2] => 36
[3] => 51
[4] => 53
[5] => 54
[6] => 55
[7] => 56
[8] => 57
[9] => 60
[10] => 61
[11] => 63
[12] => 64
[13] => 65
[14] => 66
[15] => 67
[16] => 68
[17] => 69
[18] => 74
[19] => 97
)
I want to sum the numbers in the array like that:
Array (
[0] => 34-36
[1] => 51
[2] => 53-57
[3] => 60-61
[4] => 63-69
[5] => 74
[6] => 97
)
How can I archieve that with PHP?
Logically and with a little help from php you can get the desired output
$zipArr = array ("0" => 34,"1" => 35,"2" => 36,"3" => 51,"4" => 53,"5" => 54,"6" => 55,"7" => 56,"8" => 57,"9" => 60,"10" => 61,"11" => 63,"12" => 64,"13" => 65,"14" => 66,"15" => 67,"16" => 68,"17" => 69,"18" => 74,"19" => 97,);
$range = range(10,100,10);
print_r($zipArr);
$new_arr = [];
foreach($range as $rValue){
$tmpArr = [];
foreach($zipArr as $zipValue){
if($zipValue%$rValue < 10){
$tmpArr[] = $zipValue;
}
}
if(!empty($tmpArr)){
if(count($tmpArr) > 1){
$new_arr[] =($tmpArr[0] ." - ".end($tmpArr));
} else {
$new_arr[] =($tmpArr[0]);
}
}
}
$new_arr = array_unique($new_arr);
print_r($new_arr);
output
Array
(
[0] => 34 - 36
[1] => 60 - 69
[3] => 51 - 57
[5] => 74
[6] => 97
)
Here is the logic to get required output.
$arr = array (34,35,36,51,53,54,55,56,57,60,61,63,64,65,66,67,68,69,74,97);
$sum_arr = array();
$prev_val = 0;
foreach($arr as $key => $val)
{
/* Store initial value in array */
if($key == 0)
{
$sum_arr[] = $val;
}
/* If value is not sequence */
else if(($prev_val+1) != $val)
{
/* Get last index of array and append current value */
$sum_last_idx = count($sum_arr) - 1;
/* Append value only if both values are not equal */
if($sum_arr[$sum_last_idx] != $prev_val)
{
$sum_arr[$sum_last_idx] = $sum_arr[$sum_last_idx] . '-' . $prev_val;
}
$sum_arr[] = $val;
}
/* Retain previous value */
$prev_val = $val;
}
print_r($sum_arr);
Output
Array
(
[0] => 34-36
[1] => 51
[2] => 53-57
[3] => 60-61
[4] => 63-69
[5] => 74
[6] => 97
)

Multidimensional array in a loop prepending one 0 then two 0's then tree 0's etc

Im kinda stuck on this for a few hours. I need every time the loop, loops throught the weeks adding a extra "0" to the multidimensional array.
for example this is what i wanna achievev
Array
(
[week1] => Array // Week 1 do nothing
(
[0] => 0.461718
[1] => 2.874501
[2] => 4.968576
[3] => 4.353633
[4] => 3.019554
[5] => 2.026656
[6] => 1.405584
[7] => 1.119564
[8] => 1.131822
)
[week2] => Array // Week 2 prepend one "0"
(
[0] => 0
[1] => 0.461718
[2] => 2.874501
[3] => 4.968576
[4] => 4.353633
[5] => 3.019554
[6] => 2.026656
[7] => 1.405584
[8] => 1.119564
[9] => 1.131822
)
[week3] => Array // Week 3 prepend two "0's"
(
[0] => 0
[1] => 0
[2] => 0.461718
[3] => 2.874501
[4] => 4.968576
[5] => 4.353633
[6] => 3.019554
[7] => 2.026656
[8] => 1.405584
[9] => 1.119564
[10] => 1.131822
)
[week3] => Array // Week 4 prepend three "0's"
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0.461718
[4] => 2.874501
[5] => 4.968576
[6] => 4.353633
[7] => 3.019554
[8] => 2.026656
[9] => 1.405584
[10] => 1.119564
[11] => 1.131822
)
// Etc...
This is my code.
I get now in every array only one "0"
$totaal = array();
for($w = 1; $w <= 52; $w++)
{
for($sw = 0; $sw <= 8; $sw++)
{
$totaal["week".$w][] = $vruchtzettings_week["week".$w][$sw];
}
}
for($w = 1; $w <= 52; $w++)
{
$sum = 0;
array_unshift($totaal["week".$w], $sum);
for($sw = 0; $sw <= 8; $sw++)
{
echo $totaal["week".$w][$sw]."<br />";
}
}
Thanks in advance
$increments = 0;
foreach ($weeks as &$week) {
if ($increments > 0) {
for ($i=0; $i< $increments; $i++) {
array_unshift($week, 0);
}
}
$increments++;
}
EDIT: Changed <= to < in the for loop
More in depth response:
// If I'm not mistaken $vruchtzettings_week contains all your
// data up to 52 weeks. You're first transferring this data
// to a variable, $totaal, by looping through it's contents. But
// why not just set $totaal equal to $vruchtzettings_week?
$totaal = $vruchtzettings_week; // tada!
$increments = 0;
foreach ($totaal as &$data) { // We make a reference of $data so any changes are preserved
if ($increments > 0) {
for ($i=0; $i< $increments; $i++) {
array_unshift($data, 0);
}
}
$increments++;
}
var_dump($totaal); // Total now looks like what you wanted
/*
Array
(
[week1] => Array // Week 1 do nothing
(
[0] => 0.461718
[1] => 2.874501
[2] => 4.968576
[3] => 4.353633
[4] => 3.019554
[5] => 2.026656
[6] => 1.405584
[7] => 1.119564
[8] => 1.131822
)
[week2] => Array // Week 2 prepend one "0"
(
[0] => 0
[1] => 0.461718
[2] => 2.874501
[3] => 4.968576
[4] => 4.353633
[5] => 3.019554
[6] => 2.026656
[7] => 1.405584
[8] => 1.119564
[9] => 1.131822
)
etc...
*/
In answer to the comment below: The following will fill an array with the sum of the weeks data, where each successive index includes the sum of data from all weeks previous.
$totaal_part1 = array();
foreach ($totaal as $i => $data) {
$totaal_part1[] = array_sum($data) + (isset($totaal_part1[$i - 1]) ? $totaal_part1[$i - 1] : 0);
}
This is not very elegant, but should work:
$totaal = array();
for($w = 1; $w <= 52; $w++)
{
for($sw = 0; $sw <= 8; $sw++)
{
$totaal["week".$w][] = $vruchtzettings_week["week".$w][$sw];
}
}
for($w = 1; $w <= 52; $w++)
{
$sum = 0;
for ($i = 0; $i < $w; $i++) array_unshift($totaal["week".$w], $sum);
for($sw = 0; $sw <= 8; $sw++)
{
echo $totaal["week".$w][$sw]."<br />";
}
}

Flatten Array: Keep index, value equal to position in array

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);

Categories