Select first working day from multidimensional array - php

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

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']))

PHP Looping multi dim array

Array
(
[stat] => ok
[offset] => 0
[limit] => 50
[total] => 1
[monitors] => Array
(
[monitor] => Array
(
[0] => Array
(
[id] =>
[friendlyname] =>
[url] =>
[type] => 3
[subtype] =>
[keywordtype] =>
[keywordvalue] =>
[httpusername] =>
[httppassword] =>
[port] =>
[interval] => 300
[status] => 2
[alltimeuptimeratio] => 100
[log] => Array
(
[0] => Array
(
[type] => 2
[datetime] => 11/24/2016 04:01:32
)
[responsetime] => Array
(
[0] => Array
(
[datetime] => 12/09/2016 19:34:02
[value] => 109
)
[1] => Array
(
[datetime] => 12/09/2016 19:29:02
[value] => 110
)
[2] => Array
(
[datetime] => 12/09/2016 19:24:02
[value] => 110
)
)
)
)
)
)
I need to get the value of datetime, and value from the responsetime array. I tried the following but it seems to not return anything.
foreach($multidim as $value) {
foreach($value as $key => $val) {
if($key == "responsetime") {
echo $val[3];
}
}
}
Where $multidim is the large multi-dim array listed above. Any help is appreciated as I am not sure where to go from here.
Thank you in advance.
to access all the response times you should do sth like this
foreach($multidim['monitors']['monitor'][0]['responsetime'] as $key => $value) {
//here $key will be 0,1,2,3...
//$value['datetime'] will be 11/24/2016 04:01:32...
//$value['value'] will be 109,110,...
}
that is if you just want to access all the response times of the first monitor. if you want to access all the monitors and their response times you would need 2 loops for example
foreach($multidim['monitors']['monitor'] as $monitorId => $monitorData) {
foreach($monitorData['responsetime'] as $key => $value) {
//here you can access all the variables e.g
//$monitorId will be 0,1,2,3...
//$key will be 0,1,2,3...
//$value['datetime'] will be 11/24/2016 04:01:32...
//$value['value'] will be 109,110,...
}
}
I hope that sends you in the right direction :)

php add some value on specific location in multidimensional array

I have array like this
Array ([0] => Array ( [user_id] => 21 [email] => momod#modara.com [brand] => Array ( [0] => GOFUEL_W [1] => GOFUEL_USD_W ) ) [1] => Array ( [user_id] => 22 [email] => hemisphere#modara.com [brand] => Array ( [0] => GOFUEL_W ) ) [2] => Array ( [user_id] => 23 [email] => madoka#modara.com [brand] => Array ( [0] => GOFUEL_W [1] => GOFUEL_USD_W [2] => GOFUEL_BGD_W ) ) )
i want to locate user_id 22 and put this value "GO_FUEL_SGD_W" on brand, what should i do, so the view of array will look like this
Array ([0] => Array ( [user_id] => 21 [email] => momod#modara.com [brand] => Array ( [0] => GOFUEL_W [1] => GOFUEL_USD_W ) ) [1] => Array ( [user_id] => 22 [email] => hemisphere#modara.com [brand] => Array ( [0] => GOFUEL_W => [1] =>GO_FUEL_SGD_W ) ) [2] => Array ( [user_id] => 23 [email] => madoka#modara.com [brand] => Array ( [0] => GOFUEL_W [1] => GOFUEL_USD_W [2] => GOFUEL_BGD_W ) ) )
Just use loop:
foreach($array as &$item)
{
if(array_key_exists('user_id', $item) &&
$item['user_id']==22 &&
array_key_exists('brand', $item) &&
!in_array('GO_FUEL_SGD_W', $item['brand']))
{
$item['brand'][] = 'GO_FUEL_SGD_W';
}
}
A simple foreach loop will do the job:
foreach($myarray AS &$subarray) {
if($subarray['user_id'] == 22) {
$subarray['brand'][] = "GO_FUEL_SGD_W";
break;
}
}
Working example: http://3v4l.org/8aQMj
You will need to iterate over the array and look for the element you're searching for.
foreach ($array as &$element) {
if ($element['user_id'] != 22)
continue;
$element['brand'][] = "GO_FUEL_SGD_W";
break;
}
With continue; all elements will be skipped, who have $element['user_id'] != 22 (and so none of the code after the continue; will be applied to them!).
Also it will end the loop once the requested element is reached and modified, thanks to break;.
$array= //your array;
foreach($array as $x){
if($x['user_id']=='22'){
$x['brand'][]='GO_FUEL_SGD_W';
break;
}
}

how can I select the array with the oldest time

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.

Transform Array from Key/Value to Multi Dimensional

this may seem a rather trivial question, please excuse my ignorance. Still getting the hang of array manipulation...
I have a CakePHP app that is posting an array to my controller to be saved. I need to somehow reformat the sent array so that it may be processed properly by Cake's Save behaviour.
The array posted is:
Array (
[788] => Array ( [id] => 788 )
[787] => Array ( [id] => 787 )
[786] => Array ( [id] => 0 )
[785] => Array ( [id] => 0 )
[value_1] => 0
[analysed_date] => Array (
[month] => 08
[day] => 16
[year] => 2011
)
[job_id] => 34
)
Desired Array:
Array (
[0] => Array (
[id] => 788
[value_1] => 0
[analysed_date] => Array (
[month] => 08
[day] => 16
[year] => 2011
)
)
[1] => Array (
[id] => 787
[value_1] => 0
[analysed_date] => Array (
[month] => 08
[day] => 16
[year] => 2011
)
)
)
Thanks for taking the time to look.
EDIT:
I've just realised I omitted the fact that if the array has an [id] => 0 that it needs to be ignored. This was my primary stumbling block. Apologies. I hope the edit clarifies my problem better.
SOLVED
Thank you for your help guys. I was able to come up with the solution by myself. Here is what I came up with.
foreach($org_array as $key => $value){
if(is_array($value)){
if(isset($value['id'])){
if($value['id'] != 0) {
$data[$i] = array(
'id' => $value['id'],
'value_1'=> $value_1,
'analysed_date' => $date
);
$i++;
}
}
}
}
Something like this should work, but just for your example:
$array_keys = array_keys($org_array);
$new_array = array();
foreach ($array_keys as $key)
{
if (is_int($key))
{
$new_array[] = array(
"id" => $key,
"value1" => $org_array["value1"],
"analysed_date" => $org_array["analysed_date"]
);
// you might want to loop throught the original array to get all non-integer key values instead of hard-coding it
}
}
$main = Array (
[788] => Array ( [id] => 788 )
[787] => Array ( [id] => 787 )
[786] => Array ( [id] => 786 )
[785] => Array ( [id] => 785 )
[value_1] => 0
[analysed_date] => Array (
[month] => 08
[day] => 16
[year] => 2011
)
[job_id] => 34
)
$analysed_date = $main['analysed_date'];
$value1 = $main['value_1'];
$result = array();
$i=0;
foreach($main as $key=>$value)
{
if( is_numeric($key)
{
$result[$i]=array();
$result[$i]['id']=$key;
$result[$i]['value_1']=$value1;
$result[$i]['analysed_date']=$analysed_date;
$i++;
}
}

Categories