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
)
Related
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;
}
$prices has the following values
Array
(
[0] => 824.2776
[1] => 1586.5128
[2] => 2277.8424
[3] => 2902.698
[4] => 3465.5112
[5] => 3957.4188
)
I also have some voucher codes in $vArray which looks like this:
Array
(
[voucher1] => 30
[voucher2] => 40
)
I would like to create prices which have a percentage subtracted. In this case 30% and 40% ... just not sure how.
foreach($prices as $price){
foreach($vArray as $key => $value){
$prices[] = ($value / $price) * 100;
}
}
The above foreach is creating these additional array items in $prices
[6] => 3.6395505591805
[7] => 1.8909396759988
[8] => 1.3170358054622
[9] => 1.0335212274925
[10] => 0.86567315090484
[11] => 0.75806988130748
$prices = [
100,
824.2776,
1586.5128,
2277.8424,
2902.698,
3465.5112,
3957.4188
];
$vArray = [
'voucher1' => 30,
'voucher2' => 40,
];
foreach ($prices as $price) {
foreach ($vArray as $voucherId => $percentage){
$prices[] = $price * (1 - ($percentage / 100));
}
}
print_r($prices);
Output:
Array
(
[0] => 100
[1] => 824.2776
[2] => 1586.5128
[3] => 2277.8424
[4] => 2902.698
[5] => 3465.5112
[6] => 3957.4188
[7] => 70
[8] => 60
[9] => 576.99432
[10] => 494.56656
...
[20] => 2374.45128
)
I want to split the following array into odd and even array by e.g [SUBJECT_CODE] => 05
Array
(
[0] => Array
(
[0] => English
[subject_name] => English
[1] => E-I
[subject_abr] => E-I
[2] =>
[ENROL_NO] =>
[3] => 2013
[YEAR_] => 2013
[4] => 1
[EXAM_CODE] => 1
[5] => 42701
[ROLL_NO] => 42701
[6] => 01
[SUBJECT_CODE] => 01
)
[1] => Array
(
[0] => English
[subject_name] => English
[1] => E-II
[subject_abr] => E-II
[2] => 027-B/FMSGUK-2011
[ENROL_NO] => 027-B/FMSGUK-2011
[3] => 2013
[YEAR_] => 2013
[4] => 1
[EXAM_CODE] => 1
[5] => 42701
[ROLL_NO] => 42701
[6] => 02
[SUBJECT_CODE] => 02
)
[2] => Array
(
[0] => Urdu
[subject_name] => Urdu
[1] => U-I
[subject_abr] => U-I
[2] =>
[ENROL_NO] =>
[3] => 2013
[YEAR_] => 2013
[4] => 1
[EXAM_CODE] => 1
[5] => 42701
[ROLL_NO] => 42701
[6] => 05
[SUBJECT_CODE] => 05
)
)
For this, a simple foreach should suffice. Consider this example:
// $values is your original array
$new_values = array();
foreach ($values as $key => $value) {
if($value['SUBJECT_CODE'] & 1) {
$new_values['odd'][] = $value;
} else {
$new_values['even'][] = $value;
}
}
echo '<pre>';
print_r($new_values);
echo '</pre>';
Step 1: Traverse through the array
$odd = array();
$even = array();
foreach ($arr as $key => $value) {
if ($key % 2 == 0) {
$even[] = $value;
}
else {
$odd[] = $value;
}
}
}
The odd value are stored in the $odd array and even in the $even.
You can print_r($odd)
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?
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);