Reputation: 95
I have 10 sets of numbers into array, representing different days.
<?php
$day1 = ['23', '11', '52', '33', '1', '4'];
$day2 = ['66', '70', '55', '8', '22', '1'];
$day3 = ['38', '21', '52', '51', '53', '9'];
$day4 = ['14', '31', '54', '5', '73', '39'];
$day5 = ['10', '3', '22', '59', '73', '39'];
$day6 = ['22', '13', '4', '5', '73', '39'];
$day7 = ['40', '3', '22', '5', '13', '30'];
$day8 = ['88', '53', '4', '25', '71', '19'];
$day9 = ['10', '30', '49', '25', '73', '46'];
$day10 = ['10', '3', '4', '5', '73', '11'];
I need to echo the latest time a number has been seen into the sets, considering that $day1 is the older moment, while $day10 is the most recent.
Example : number 73 appears in day 4, 5, 6, 9 and 10, but I'm interested only in the most recent one so I should print
73 last seen on day 10
I would like to be able also to print aggregate values, like this :
last seen since day 10 : '10', '3', '4', '5', '73', '11'
last seen since day 9 : '30', '49', '25', '46'
last seen since day 8 : '88', '53', '71', '19'
last seen since day 7 : '40', '22'. '13'
I'm really new to php, so i was trying by exploding the values, then introducing a foreach cycle
enter $myArray1 = explode(" " $day1);
$myArray2 = explode(" " $day2);
$myArray3 = explode(" " $day3);
$myArray4 = explode(" " $day4);
$myArray5 = explode(" " $day5);
$myArray6 = explode(" " $day6);
$myArray7 = explode(" " $day7);
$myArray8 = explode(" " $day8);
$myArray9 = explode(" " $day9);
$myArray10 = explode(" " $day10);
print_r ($myArray1);
foreach ($myArray1 as $value);
{
echo "$value <br>";
$array[] = $value;
}
But I'm definetely clueless. Can anyone help me ? thanks
Upvotes: 0
Views: 65
Reputation: 78994
First off, use an array of arrays with the day number as the index:
$days[1] = ['23', '11', '52', '33', '1', '4'];
$days[2] = ['66', '70', '55', '8', '22', '1'];
$days[3] = ['38', '21', '52', '51', '53', '9'];
Then something like:
$find = '1';
$latest = 'never';
foreach($days as $day => $values) {
if(in_array($find, $values)) {
$latest = $day;
}
}
echo "$find last seen on day $latest";
To get aggregates you might loop and build an array indexed on the value with the latest day as the array value:
$aggregate = [];
foreach($days as $day => $values) {
$aggregate = array_replace($aggregate, array_fill_keys($values, $day));
}
Will result in something like:
(
[23] => 1
[11] => 1
[52] => 3
[33] => 1
[1] => 2
[4] => 1
[66] => 2
[70] => 2
[55] => 2
[8] => 2
[22] => 2
[38] => 3
[21] => 3
[51] => 3
[53] => 3
[9] => 3
)
That you can use by getting the indexes for the elements that have day as the value:
echo "last seen day 1" . implode(', ', array_keys($aggregate, '1'));
Then you can use that for the first part as well:
echo "1 last seen on day {$aggregate[1]}";
Upvotes: 1
Reputation: 8751
// First of all, convert days to 2D array.
$dayList = [];
$dayList[] = $day1;
$dayList[] = $day2;
$dayList[] = ...;
// Cache last-seen time for each number.
$lastSeen = [];
foreach ($dayList as $dayId => $day) {
foreach ($day as $number) {
$lastSeen[$number] = $dayId;
}
}
// At last, maybe sort the array, but now we have a list,
// with numbers as key and last-seen day-index as value
// (so, use plus one to get number instead of index).
echo '73 was last seen at day:' . ($lastSeen[73] + 1);
print_r($lastSeen);
Upvotes: 0