how can I select the array with the oldest time - php

I have these arras
[0] => Array
(
[TEAM] => Array
(
[id] => 5
[name] => localhost
)
[Registraion] => Array
(
[Registered] => 2011-09-20 09:20:51
)
)
[1] => Array
(
[TEAM] => Array
(
[id] => 6
[name] => localhost
)
[Registraion] => Array
(
[Registered] => 2011-09-20 09:30:51
)
)
[2] => Array
(
[TEAM] => Array
(
[id] => 7
[name] => localhost
)
[Registraion] => Array
(
[Registered] => 2011-09-20 09:40:51
)
)
I want to get this
[0] => Array
(
[TEAM] => Array
(
[id] => 5
[name] => localhost
)
[Registraion] => Array
(
[Registered] => 2011-09-20 09:20:51
)
)
as that person is the oldest to register.
How can I get the oldest registration value?
thanks

$oldestkey = null;
foreach (array_keys($array) as $key) {
if (isnull($oldestkey) || ($array[$key]['Registraion']['Registered'] < $array[$oldestkey]['Registraion']['Registered']) {
$oldestkey = $key;
}
}
Note that your key Registraion is mis-spelled, I'm guessing it should be Registration? Also note that this code will not handle the case where there's multiple keys with the same registration time. It'll pick out the FIRST oldest time and return the key for that record. Any duplicate times will be ignored.

Loop each item
$oldest = $arr[0];
foreach($array as $arr){
if($arr["Registration"]["Registered"] < $oldest["Registration"]["Registered"])
$oldest = $arr;
}
Please use the time comparision while comparing

function getOldestRecord($ar)
{
$last_id;
$last_time = 0;
foreach($ar as $key => $val)
{
$time_stamp = strtotime($val['Registration']['Registered']);
if($time_stamp > $last_time)
{
$last_time = $time_stamp;
$last_id = $key;
}
}
return $ar[$last_id];
}
function above accepts your array, then loops through it and compare dates, and it will return last registered user.

Related

Increment count for value in foreach loop PHP

I am trying to count the instances of rfid from one array to insert into another array so I can use this data to create charts.
At the moment my code is as follows
if ($message == "Broken" && $message_type == "BreakBeam" && $previous==$cat_id) {
$total = 1;
$splitTimeStamp = explode(" ",$eventtime);
$date = $splitTimeStamp[0];
$time = $splitTimeStamp[1];
$events[$c]['date'] = $date;
$events[$c]['device'] = $device;
$events[$c]['time']= $time;
$events[$c]['rfid']= $previous;
$events[$c]['count']=$total;
$c++;
}
}
$a = array();
$i=0;
foreach($events as $event){
if(isset($a[$event['rfid']])){
$a['rfid_id'][$event['rfid']]++;
}else{
$a['rfid_id'][$event['rfid']]=1;
}
if(isset($a[$event['date']])){
$a['dateinsert'][$event['date']]++;
}else{
$a['dateinsert'][$event['date']] =1;
}
}
$rfid = array();
// those are the ones we're going to put in!
foreach($a as $key => $count) {
foreach($count as $eventdetails['rfid_id'] => $event){
// so $key is the [rfid] key of the entry and $count is the count (all 1's?) in it
if (isset($rfid[$key])) {
$rfid[$key]+=$event;
}
else {
$rfid[$key]=$event;
}
}
}
Which outputs as follows :
Array
(
[0] => Array
(
[date] => 2020-09-17
[device] => 2
[time] => 15:51:37
[rfid] => 23641
[count] => 1
)
[1] => Array
(
[date] => 2020-09-17
[device] => 2
[time] => 15:52:20
[rfid] => 5609
[count] => 1
)
[2] => Array
(
[date] => 2020-09-17
[device] => 2
[time] => 15:53:23
[rfid] => 5609
[count] => 1
)
[3] => Array
(
[date] => 2020-09-17
[device] => 2
[time] => 16:02:44
[rfid] => 5609
[count] => 1
)
)
Array
(
[rfid_id] => Array
(
[23641] => 1
[5609] => 1
)
[dateinsert] => Array
(
[2020-09-17] => 1
)
)
Array
(
[rfid_id] => 2
[dateinsert] => 1
)
Ideally, I would like to achieve the following :
Array
(
[rfid_id] => Array
(
[23641] => 1
[5609] => 3
)
[dateinsert] => Array
(
[2020-09-17] => 1
)
)
or something to that effect, where I can look at the date, rfid codes and the time each was read on that date.
You simply missing the path in the assignment loop.
Same goes for rfid and date so I just explain on rfid.
You check the isset on one path but is not exist set on another - this cause the path to never exist - that why you keep getting 1's.
Look at:
foreach($events as $event){
if(isset($a[$event['rfid']])){
$a['rfid_id'][$event['rfid']]++;
} else {
$a['rfid_id'][$event['rfid']]=1;
}
}
Notice $a[$event['rfid']] is NOT the same as $a['rfid_id'][$event['rfid']].
You should change the if statement to be: if (isset($a['rfid_id'][$event['rfid']))

Select first working day from multidimensional array

I have a multidimensional array as shown below. in the day's array, it has various days which has both working and non-working days. now I want to consider first type=" working" as the start_date.
could you help me. thanks
Array
(
[error] => 0
[data] => Array
(
[start_date] => 2018-03-11
[end_date] => 2018-03-21
[days] => Array
(
[0] => Array
(
[type] => non_working
[sub_type] => weekend
[sub_sub_type] =>
[date] => 2018-03-11
)
[1] => Array
(
[type] => working
[sub_type] =>
[sub_sub_type] =>
[full_date] => 2018-03-12
)
[2] => Array
(
[type] => working
[sub_type] =>
[sub_sub_type] =>
[full_date] => 2018-03-13
)
)
)
)
I've tries this as of now:
$i=0;
$var = array();
foreach($arr['data']['days'][$i] as $var) {
if($var['type'] == 'working') {
break;
}
}
Rework $arr['data']['days'][$i] to $arr['data']['days']:
$start = null;
foreach($arr['data']['days'] as $var) {
if($var['type'] == 'working') {
$start = $var['date'];
break;
}
}

PHP Multidimensional sort?

I have a rather annoying array structure to work with and I need to sort it by any arbitrary key combination. 2 records are displayed below but multiple records with or without the same structure will be present when sorting is actioned.
Here are two records.
Array(
[0] => Array
(
[cid] => 1
[title] => Mr
[first_name] => Abet
[last_name] => Simbad
[emails] => Array
(
[374] => Array
(
[eid] => 374
[name] => ski lodge
[email] => simbad#skifree.com
)
[373] => Array
(
[eid] => 373
[name] => work
[email] => simbad#work.com
)
[375] => Array
(
[eid] => 375
[name] => personal
[email] => simbad#gmail.com
)
)
)
[1] => Array
(
[cid] => 2
[title] => Mrs
[first_name] => Angie
[last_name] => Stokes
[emails] => Array
(
[590] => Array
(
[eid] => 590
[name] => work
[email] => angie#gmail.com
)
)
)
So if I wanted to sort by email in ascending order in the emails array, how can I get the second complete record to come first in the result array? angie#gmail.com comes before simbad#....
Also Some records will not contain an emails array. They would be last in the result set.
Any help would be much appreciated.
The array shown is a cut down version but I have addresses, notes, phones and websites in the same annoying structure. Ideally I could sort with something like
$sort = array('emails','email')
$data = sort_data_func('ASC',$sort,$data);
But anything steps in the right direction will help. :)
Here's some code I have so far
$sort = array('emails','email');
foreach($contacts as $ckey => $c){
if(is_array($c[$sort[0]])){
foreach($c[$sort[0]] as $key1 => $sort0){
if($sort0[$sort[1]]!=''){
$res[$sort[0]][$ckey][$sort[1]][] = $sort0[$sort[1]];
}
}
}
}
print_r($res);
Which produces:
Array
(
[emails] => Array
(
[0] => Array
(
[0] => simbad#skifree.com
[1] => simbad#work.com
[2] => simbad#gmail.com
)
[1] => Array
(
[0] => angie#gmail.com
)
)
)
But I have no idea where to go from here.
EDIT
OK I have the records in the currect order now but how can I keep the initial record ID in the resulting array?
Here's what I'm using.
$direction=='ASC'
function cmp_asc($a, $b){
$key = current(array_keys($a));
sort($a[$key]);
$a[$key] = current($a[$key]);
sort($b[$key]);
$b[$key] = current($b[$key]);
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
function cmp_desc($a, $b){
$key = current(array_keys($a));
asort($a[$key]);
$a[$key] = current($a[$key]);
asort($b[$key]);
$b[$key] = current($b[$key]);
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
if($direction=='ASC'){
usort($res[$sort[0]], 'cmp_asc');
}else{
usort($res[$sort[0]], 'cmp_desc');
}
In
Array
(
[emails] => Array
(
[0] => Array
(
[email] => Array
(
[0] => simbad#skifree.com
[1] => simbad#work.com
[2] => simbad#gmail.com
)
)
[1] => Array
(
[email] => Array
(
[0] => angie#gmail.com
)
)
)
)
Out
Array
(
[emails] => Array
(
[0] => Array
(
[email] => Array
(
[0] => angie#gmnail.com
)
)
[1] => Array
(
[email] => Array
(
[0] => simbad#skifree.com
[1] => simbad#work.com
[2] => simbad#gmail.com
)
)
)
)
One of the usort functions, combined with a self-written comparison function that detects the order in which two elements should be sorted, should do the trick.

PHP combine array based on value of particular element

I have an array that looks like this
Array (
[0] => Array
(
[id] => 123
[count] => 1
)
[1] => Array
(
[id] => 123
[count] => 3
)
[2] => Array
(
[id] => 513
[count] => 0
)
[3] => Array
(
[id] => 561
[count] => 1
)
[4] => Array
(
[id] => 613
[count] => 7
)
)
What I want to do is create a new array, that totals the count where the id values are the same. So for example, the new array would look like this:
Array (
[0] => Array
(
[id] => 123
[count] => 4
)
[1] => Array
(
[id] => 513
[count] => 0
)
[2] => Array
(
[id] => 561
[count] => 1
)
[3] => Array
(
[id] => 613
[count] => 7
)
)
Would anyone know a good method to do this?
Thank you!
Short and simple:
$new_array = array();
foreach($data_array as $value) {
if(array_key_exists($value['id'], $new_array)) {
$new_array[$value['id']] += $value['count'];
} else {
$new_array[$value['id']] = $value['count'];
}
}
echo print_r($new_array,true);
I made it id => value since there didn't seem to need another array for data when the id could be used as the array's key.
Any reason why you are not making an associative array of id => count
You can do it with the following function (I didn't tested the code, but it should work ):
function get_count_array($arr) {
$new_array = array();
foreach($arr as $item) {
$found = false;
//loop through all the current new items to check if we already have it
for($i = 0; $i < count($new_array); $i++) {
//do we have it?
if($new_array[$i]['id'] == $item['id']) {
$new_array[$i]['count'] += 1;
$found = true;
break;
}
}
if(!$found) {
$new_array[] = array('id' => $item['id'], 'count' => 0);
}
}
return $new_array;
}

Looping through array and totalling values

I require a bit of assistance, if someone would be kind enough to help.
I have an array with values, which I want to loop through, and if any of the 'user_id values' are the same then to total the 'max_score' value of the duplicate user id's.
Array
(
[0] => Array
(
[user_id] => 2
[max_score] => 10081
)
[1] => Array
(
[user_id] => 1
[max_score] => 8774
)
[2] => Array
(
[user_id] => 2
[max_score] => 5477
)
[3] => Array
(
[user_id] => 3
[max_score] => 5267
)
[4] => Array
(
[user_id] => 1
[max_score] => 5010
)
)
Would anyone know how to accomplish this?
Many thanks.
$totals = array();
foreach ($your_array_values as $v) {
$id = $v['user_id'];
if (isset($totals[$id])) {
$totals[$id] += $v['max_score'];
} else {
$totals[$id] = $v['max_score'];
}
}
you need a second array with the user ids as key. You can do it like this:
$scoresums = array();
foreach ($yourarray AS $user_score) {
if (!isset($scoresums[$user_score['user_id']])) $scoresums[$user_score['user_id']] = 0;
$scoresums[$user_score['user_id']] += $user_score['max_score'];
}
The third line will prevent php from throwing notices.

Categories