How to convert array to string in laravel - php

$date = gc_head::select('DATE','point_no')->orderBy('DOCDATE','desc')->limit(7)->get();
This return something like array format
[{"DATE":"2021-06-11 00:00:00","point_no":"SRT-358"},{"DATE":"2021-06-11 00:00:00","point_no":"SRT-359"},{"DATE":"2021-06-11 00:00:00","point_no":"SRT-360"},{"DATE":"2021-06-10 00:00:00","point_no":"SRT-357"},{"DATE":"2021-06-10 00:00:00","point_no":"SRT-356"},{"DATE":"2021-06-10 00:00:00","point_no":"SRT-355"},{"DATE":"2021-06-09 00:00:00","point_no":"SRT-348"}]
I want to isolate DATE and point_no. For example
$year=['2015','2016','2017','2018','2019','2020'];
I tried $date['DOCDATE'] to isolate date it's not working. Is there any better way to fix this??

$date = gc_head::select('DATE','point_no')->orderBy('DOCDATE','desc')->limit(7)->get();
$date_array = array();
foreach($date as $dat){
array_push($date_array, \Carbon\Carbon::parse($dat["DATE"])->format('Y');
}
return $date_array;

You can use array_map function.
$extractYear = function($date) {
return substr($date["DATE"], 0, 4);
};
$years = array_map($extractYear, $date);

try this query -
$years= gc_head::select('DATE','point_no')->orderBy('DOCDATE','desc')->limit(7)->get()->map(function ($q) {
return [Carbon::parse($q->DATE)->format('Y')];
});

Related

How to remove values from an array in Laravel

I am trying to make a really basic day booking system and need to return all dates within a range, and then remove selected dates from that range. I tried the following code but realised that will remove duplicates which is fine, but I also need that date to be removed too.
Can anyone suggest a good way of doing this?
In the below example I am just hoping to see:
2022-04-03T00:00:00.000000Z
2022-04-04T00:00:00.000000Z
2022-04-05T00:00:00.000000Z
$start_date = "2022-04-01";
$end_date = "2022-04-05";
$datesToRemove = [
'2022-04-01T00:00:00.000000Z',
'2022-04-02T00:00:00.000000Z'
];
$range = Carbon::parse($start_date)->toPeriod($end_date)->toArray();
$available = array_unique(array_merge($range, $datesToRemove));
return $available;
To compare it is necessary to have the compared values in the same format. I decide to morph the $datesToRemove to Carbon format. The you can use to nested loops and check with PHP in_array() function.
$start_date = "2022-04-01";
$end_date = "2022-04-05";
$datesToRemove = [
"2022-04-01T00:00:00.000000Z",
"2022-04-02T00:00:00.000000Z"
];
$range = \Carbon\Carbon::parse($start_date)->toPeriod($end_date)->toArray();
$datesToRemove2 = [];
foreach($datesToRemove as $r) {
$datesToRemove2[] = \Carbon\Carbon::parse($r);
}
$res = [];
foreach($datesToRemove2 as $index => $d1) {
if(in_array($d1, $range)) {
unset($range[$index]);
}
}
return $range;
output
{
"2":"2022-04-03T00:00:00.000000Z",
"3":"2022-04-04T00:00:00.000000Z",
"4":"2022-04-05T00:00:00.000000Z"
}
Means that

Php filter multidimensional array

I have array like this:
arr=[
627=[
'lead_data'=>['name'=>'Name1', 'date'=>'2019-04-09']
],
500=[
'lead_data'=>['name'=>'Name2', 'date'=>'2018-05-07']
],
534=[
'lead_data'=>['name'=>'Name3', 'date'=>'2019-07-10']
],
100=[
'lead_data'=>['name'=>'Name4', 'date'=>'2019-05-12']
],
]
How can I filter this array where date is between 2019-05-01 and 2019-07-12.
So in result there will be elements with ids 534 and 100.
Or date is >= 2019-07-05 or date is <= 2019-01-01 ?
I know there is array_filter function, but cant understand how to use it in thus case? Please help, thanks
The simplest solution would to just iterate over your data like so:
<?php
$begin = date('Y-m-d', strtotime("2019-05-01"));
$end = date('Y-m-d', strtotime("2019-07-12"));
foreach($array as $key => $data)
{
$date = date('Y-m-d', strtotime($$data['date']));
if (($$data > $begin) && ($date < $end)){
unset($array[$key]);
}
}
var_dump($array);
Always make sure you check out official documentation on php.net because it usually have tons of examples and very thorough explanations.
In your case you can compare dates as strings (since they are in Y-m-d format and comparing them alphabetically will do the trick):
$filtered = array_filter($arr, function ($item) {
return ($item['lead_data']['date'] > '2019-05-01') && ($item['lead_data']['date'] < '2019-07-12');
});
By using array_filter(), and using the use keyword, you can supply variables into the filter - this can be the start- and end-dates for your limits.
When using array_filter(), the data will remain in the array if the return value inside the callback is true, otherwise it will be removed. So then compare the dates, and see if they are greater than $from, and less than $to.
$from = '2019-05-01';
$to = '2019-07-12';
$result = array_filter($arr, function ($v) use ($from, $to) {
return $v['lead_data']['date'] > $from && $v['lead_data']['date'] < $to;
});
print_r($result);
Live demo at https://3v4l.org/Cmt8H

Use a json decode array without foreach

I have a following json:
$json = '[{"name":"Peter","bday":"1990-10-10"},{"name":"Mark","bday":"1992-08-10"},{"name":"John","bday":"1993-08-09"},{"name":"John","bday":"2000-05-19"}]';
Are names and Birthdays. Is there any way to use a criteria to search birthdays without foreach?
For example, "all registry with birthdays after 1993-01-01".
Thanks
I was able to do this with array_filter function
function greater_than($arr){
$date = $arr['bday'];
if(strtotime($date) > strtotime('1993-01-01')){
return $date;
}
}
$json = '[{"name":"Peter","bday":"1990-10-10"},{"name":"Mark","bday":"1992-08-10"},{"name":"John","bday":"1993-08-09"},{"name":"John","bday":"2000-05-19"}]';
$dec = json_decode($json,true);
print_r(array_filter($dec, "greater_than"));

Compare two array using laravel

Hi everyone I have an array
$dates = [2016-11-02,2016-11-05,2016-11-11,2016-11-15,2016-11-16]
in this I am getting all dates now I want to compare with $currentdate (2016-12-16) like this
if($dates <= $currentdate){
echo "true";
} else{
echo "false";
}
Any help will be much appreciated.
Not sure about other answers, but if you are trying to validate all the dates, please use the below code... Should work :)
$isLess = false;
$currentDate = Carbon::now();
foreach($dates as $data) {
if(Carbon::parse($date)->lte(currentDate)){
$isLess = true;
break;
}
}
echo $isLess;
Let me know in the comments if you face any problem :)
you can parse it to Carbon and use its comparison function
$date = Carbon::parse($date);
if($date->lte(Carbon::today())){
// your code
}
Here
lte() means less than equal to
to know more about Comparison operator of carbon visit http://carbon.nesbot.com/docs/#api-comparison
Since you're using Laravel, you can do that with Carbon:
if (Carbon::now()->gte(Carbon::parse($oneDate))
In one line (albeit not the prettiest)
if (collect(collect($dates)->filter(function($date) use($currentDate) { return Carbon::parse($date)->lte($currentDate); }))->count > 0) {
}

How to use implode function manualy?

My array contain date $a1[0] day $a1[1] month $a1[2] year I want the result as year/day/month give me one solution.
Below is my code
<?php
$a1 = array("01","10","2012");
$result = implode("/",$a1);
print $result;
?>
This will print 01/10/2012 but I want this result 2012/01/10. How can I take the array manualy using implode function?
How can I take year first day second and month third?
You can't use implode alone for that matter, if you are having exactly this pattern (elements order are inverted) the use this code (YYYY/MM/DD):
$a1 = array("01", "10", "2012");
$result = implode("/", array_reverse($a1));
print $result;
Else use a regex (YYYY/MM/DD):
$a1 = array("01", "10", "2012");
$result = preg_replace('/(\d{2})\/(\d{2})\/(\d{4})/', '$3/$2/$1', implode("/", $a1));
print $result;
If you need the format of (YYYY/DD/MM) then use this one:
$a1 = array("01", "10", "2012");
$result = preg_replace('/(\d{2})\/(\d{2})\/(\d{4})/', '$3/$1/$2', implode("/", $a1));
print $result;
You can use array_reverse like this:
$result = implode("/", array_reverse($a1));
This will create a new array in the reverse order.
Use the mktime function. If you have a $timestamp, you can format is as you like with the date function:
$timestamp = mktime(0, 0, 0, $a1[1], $a1[0], $a1[2]);
echo date('y/m/d', $timestamp);
You can also use a loop (manual implode):
$a1 = array("01","10","2012");
$a1_length = sizeof($a1);
$date = '';
for ($i = $a1_length; $i > 0; $i--) {
$date .= $a1[$i] . '/';
}
$date = rtrim($date, '/');
echo $date;

Categories