Build an array from 3 arrays with PHP - 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];
}

Related

How to add values to an array inside an array obtained through AJAX

In my PHP file, I'm receiving a total of 4 variables $data, $date, $shift and $val1.
$data is an array and the other 3 are date and 2 strings obtained through AJAX with no problem.
What I'm trying to do is to insert these 3 values inside my $data variable.
I tried using array merge, and a For each loop with multiple instances but no luck so far.
I obtained my variables like this:
if (isset($_POST['date'])){
$date = $_POST['date'];
$date = json_encode($date);
$date = json_decode($date);
}
if (isset($_POST['shift'])){
$shift = $_POST['shift'];
$shift = json_encode($shift);
$shift = json_decode($shift);
}
if (isset($_POST['val1'])){
$val1 = $_POST['val1'];
$val1 = json_encode($val1);
$val1 = json_decode($val1);
}
if (isset($_POST['data'])){
$dat = $_POST['data'];
$data = json_decode($dat, true);
}
$values = array($date,$shift,$val1);
$r = (array_merge($data, $values));
My data array looks something like this:
Array (
[0] => Array (
[data] => Array (
[0] => Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 0
[4] => Mat1
[5] => Box1
[6] => 100
[7] => 100
[8] => Piece1
[9] => Loc1
[10] => Mach1
[11] => 1000
[12] => Accepted
)
)
)
[1] => 2019-04-09
[2] => First
[3] => Value1
)
But what I want to achieve is this:
Array (
[0] => Array (
[data] => Array (
[0] => Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 0
[4] => Mat1
[5] => Box1
[6] => 100
[7] => 100
[8] => Piece 1
[9] => Suc1
[10] => Mach1
[11] => 1000
[12] => Accepted
[13] => 2019-04-09
[14] => First
[15] => Value1
)
)
)
)
What am I doing wrong? Or How can I achieve what I'm trying to do?
Edit: Since I can get more than one array at my array, something like this
Array (
[0] => Array (
[data] => Array (
[0] => Array (...)
[1] => Array (...)
[2] => Array (...)
[3] => Array (...)
)
)
)
I just added this code to #HelgeB answer, I'm leaving it here in case someone might need it in the future.
$count = count($data[0]['data']);
for ($i=0; $i < $count ; $i++) {
$data[0]['data'][$i][] = $date;
$data[0]['data'][$i][] = $shift;
$data[0]['data'][$i][] = $val1;
}
As far as I can see from your merged output, your $data array structure is $data[0]['data'][0] = [1,2,3,...,'Accepted'].
So in my opinion you need to insert the values exactly on the level $data[0]['data'][0] to obtain your result.
The simplest way to achieve this would be:
$data[0]['data'][0][] = $date;
$data[0]['data'][0][] = $shift;
$data[0]['data'][0][] = $val1;
If you want to use your merge approach you need to merge on the correct level like this:
$r = [0 => ['data' => [0 => (array_merge($data[0]['data'][0], $values))]]];

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.

Sorting an array of arrays by specific index descending [duplicate]

This question already has answers here:
Sort an array of associative arrays by column value
(23 answers)
Closed 5 years ago.
I have the array below
Array
(
[Prod1] => Array
(
[0] => $1,167,788.03
[1] => 26,872
[2] => 73.42
[3] => 19.0%
[4] => $1,134,106.83
[5] => $1,681,843.02
[6] => $3,098.65
[7] => $42.20
[8] => $-19.55
[9] => $-9.60
[10] => $43.46
[11] => 0.97
)
[Prod2] => Array
(
[0] => $6,730.84
[1] => 161
[2] => 0.44
[3] => 13.7%
[4] => $4,783.41
[5] => $6,755.61
[6] => $13.07
[7] => $29.71
[8] => $-27.30
[9] => $-21.50
[10] => $41.81
[11] => 0.71
)
[Prod3] => Array
(
[0] => $2,498,984.47
[1] => 30,409
[2] => 83.08
[3] => 21.5%
[4] => $3,026,866.16
[5] => $3,850,645.25
[6] => $8,270.13
[7] => $99.54
[8] => $-21.33
[9] => $-8.19
[10] => $82.18
[11] => 1.21
)
}
I am trying to sort it on descending order based of the index[0] and tried using different PHP built in functions but I was not successful on that.
Basically the desired result would be in the following order Prod3, Prod1, Prod2.
What would be the best way for solution for this?
Thanks
supposing $arr is the array you've displayed above, the below code should work in your case
$tmp_arr = array();
$sorted_arr = array();
foreach ($arr as $key => $val_array) {
//removing $ and ,
$first_index_without_dollar = str_replace(array("$", ","), array("", ""), $val_array[0]);
//getting string as number in order to sort
$first_index_without_dollar = number_format((float) $first_index_without_dollar, 2, '.', '');
$tmp_arr[$first_index_without_dollar] = $key;
}
//sorting by key descending
krsort($tmp_arr);
foreach ($tmp_arr as $val) {
$sorted_arr[$val] = $arr[$val];
}
I don't know if usort is the best way but the user function is fairly simple:
usort($array, function ($a, $b) {
$x = (int)str_replace(['$',',','.'],'',$a[0];
$y = (int)str_replace(['$',',','.'],'',$b[0];
if ($x === $y) return 0;
return ($x < $y) ? 1 : -1;
});
Edit: I missed the format on the values the first time. And I would not have found it without Mohamad Attat's answer. The str_replace calls should fix it, iff you always have two decimals in your dollar amounts.

Pair all elements between them without duplicates

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?

Comparison of two strings in PHP, and array_slice

I feel like I've done something very silly, but I can't work out what it is.
I'm running through an array of arrays generated by fgetcsv(). What I want is to split the array in two at the spot where the value in one line differs from the one in the next.
This code results in $first holding all but one of the arrays, and $second holding the last - completely ignoring the string comparison. strcmp() results in the same thing.
$csv = array();
$file = fopen('A.csv', 'r');
while (($result = fgetcsv($file)) !== false)
{
$csv[] = $result;
}
fclose($file);
array_shift($csv); //gets rid of the CSV headers
$rows = count($csv);
for ($i = 0; $i <= $rows; $i++){
if ($csv[$i][1] != $csv[$i+1][1]){
$first = array_slice($csv, 0, $i);
$second = array_slice($csv, $i);
}
}
Here's an example of the CSV file:
NAME,MATCHNAME,CHROMOSOME,START LOCATION,END LOCATION,CENTIMORGANS,MATCHING SNPS
A,person_one,2,20945970,23287731,2.48,500
A,person_one,2,233444593,234432885,1.56,500
A,person_one,4,99184637,100861136,1.24,500
A,person_two,1,154990798,157871980,2.8,700 //Here's where the new array should start
A,person_two,1,67136078,70785393,2.28,800
EDIT: My expected $first for this example would be:
Array
(
[0] => Array
(
[0] => A
[1] => person_one
[2] => 2
[3] => 20945970
[4] => 23287731
[5] => 2.48
[6] => 500
)
[1] => Array
(
[0] => A
[1] => person_one
[2] => 2
[3] => 233444593
[4] => 234432885
[5] => 1.56
[6] => 500
)
[2] => Array
(
[0] => A
[1] => person_one
[2] => 4
[3] => 99184637
[4] => 100861136
[5] => 1.24
[6] => 500
)
)
And my expected $second would be:
Array
(
[0] => Array
(
[0] => A
[1] => person_two
[2] => 1
[3] => 154990798
[4] => 157871980
[5] => 2.8
[6] => 700
)
[1] => Array
(
[0] => A
[1] => person_two
[2] => 1
[3] => 67136078
[4] => 70785393
[5] => 2.28
[6] => 800
)
)
It is doing what you want, but when it gets to the last iteration, it compares whatever's in the last line with null, so it overwrites $first and $second.
Try adding break; after the assignments to break out of the loop when the condition is met.

Categories