I am working with laravel framework and I have a nested array in which to display a subset of results. For example, on the array below I display multi task per year.
Array
(
[0] => years_tasks_section_grid
(
[0] => 2022
[1] => tasks_grid
(
[0] => Task1 2022
[1] => Task2 2022
[2] => Task3 2022
[3] => Task4 2022
[4] => Task5 2022
[5] => Task6 2022
)
)
[1] => years_tasks_section_grid
(
[0] => 2021
[1] => tasks_grid
(
[0] => Task1 2021
[1] => Task2 2021
[2] => Task3 2021
)
)
)
The problem I want to display the last 3 tasks from the 2nd level of nested array. Basically, based on my example I try to get this (only tasks of 2022):
[0] => Task1 2022
[1] => Task2 2022
[2] => Task3 2022
But I am getting this (Basically, I am getting both 3 last tasks 2022 and 2021 and probably I will have other tasks of the future years):
[0] => Task1 2022
[1] => Task2 2022
[2] => Task3 2022
[0] => Task1 2021
[1] => Task2 2021
[2] => Task3 2021
This is my code in Laravel blade (I used array_slice but is not working because I have the result as above):
#foreach($sections['years_tasks_section_grid'] as $year)
#foreach(array_slice($year['tasks_grid'], 0, 5) as $task)
<div class="col-md-4 col-xl-2 d-flex align-items-center justify-content-center justify-content-md-start mb-7 mb-xl-0 px-0">
// Other code
</div>
#endforeach
#endforeach
Any ideas?
You could use a $counter, initialized at 0 and wrap it around your div. First, let's create the variable:
#php
$counter = 1
#endphp
and now, let's wrap it around your div:
#foreach($sections['years_tasks_section_grid'] as $year)
#foreach(array_slice($year['tasks_grid'], 0, 5) as $task)
#if ($counter++ < 3)
<div class="col-md-4 col-xl-2 d-flex align-items-center justify-content-center justify-content-md-start mb-7 mb-xl-0 px-0">
// Other code
</div>
#endif
#endforeach
#endforeach
Related
The current output is..
Array
(
[0] => Array
(
[ID] => 20
[name] => Peter
[datetime] => Tuesday 26th Oct 21 3:50am
)
[1] => Array
(
[ID] => 21
[name] => Paul
[datetime] => Tuesday 26th Oct 21 4:44am
)
)
I would like the array output to be..
Array
(
[20] => Array
(
[ID] => 20
[name] => Peter
[datetime] => Tuesday 26th Oct 21 3:50am
)
[21] => Array
(
[ID] => 21
[name] => Paul
[datetime] => Tuesday 26th Oct 21 4:44am
)
)
The code I am currently using to generate the array is..
$sql=mysqli_query($conn,"SELECT * FROM `live`");
/*every time it fetches the row, adds it to array...*/
while($liveuserdata[]=mysqli_fetch_array($sql, MYSQLI_ASSOC));
I can't show you what i've tried as I don't know where to begin dispite several rephrased searches :-/
It is as simple as:
$sql = mysqli_query($conn,"SELECT * FROM `live`");
$liveuserdata = [];
while ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)) {
$liveuserdata[$row['ID']] = $row;
}
I have 3 arrays.
$eldest_range
$second_range
$third_range
$eldest_range = range($start_year1,$end_year1);
print_r($eldest_range);
$second_range = range($start_year2,$end_year2);
print_r($second_range);
$third_range = range($start_year3,$end_year3);
print_r($third_range);
Which returns:
<br> Array ( [0] => 2030 [1] => 2031 [2] => 2032 [3] => 2033 [4] => 2034 [5] => 2035 ) <Br> Array ( [0] => 2032 [1] => 2033 [2] => 2034 [3] => 2035 [4] => 2036 [5] => 2037 ) <br> Array ( [0] => 2034 [1] => 2035 [2] => 2036 [3] => 2037 [4] => 2038 [5] => 2039 ) <Br>
I want compare these arrays. To check what if any of them are present within one another. So in the above case 1 & 2 share 2032, but 1,2 & 3 share 2034.
I would rather check them separately in pairs and then as a three. (1 & 2 - 1 & 3 - 2 & 3 - 1, 2 & 3)
Here's what I have tried. This is for Eldest to second:
$result=array_intersect_assoc($eldest_range, $second_range);
print_r($result);
which returns, nothing: array()
and I have also tried..
$result=array_intersect_key($eldest_range, $second_range);
print_r($result);
which returns just the whole of $eldest_range
Array ( [0] => 2030 [1] => 2031 [2] => 2032 [3] => 2033 [4] => 2034 [5] => 2035 )
Am i using the wrong array_intersect(). I just want to see any values that are in 2 arrays. and 3 arrays (as above).
Thank you
Sometimes it is very useful to read a manual, which says that array_intersect_assoc
Computes the intersection of arrays with additional index check
Do you need this index check? Definitely no.
What you need is simple array_intersect:
$eldest_range = range(2030, 2035);
$second_range = range(2032, 2037);
$third_range = range(2034, 2039);
print_r(array_intersect($eldest_range, $second_range)); // [2032, 2033, 2034, 2035]
print_r(array_intersect($eldest_range, $second_range, $third_range)); // [2034, 2035]
I have a MySQL query that returns a dump of data like so (duplicates expected and needed for other columns that are excluded for this question):
SessionID DayofWeek SessionDetails Description
1 Sunday 1:00 PM Foo
1 Sunday 1:00 PM Foo
1 Sunday 1:00 PM Foo
1 Sunday 1:00 PM Foo
2 Monday 10:00 AM Foo
2 Monday 10:00 AM Foo
2 Monday 10:00 AM Foo
2 Monday 10:00 AM Foo
3 Monday 7:00 PM Barr
3 Monday 7:00 PM Barr
3 Monday 7:00 PM Barr
3 Monday 7:00 PM Barr
I am trying to create an array for each day and only store the different session/times once in it, e.g.,
Sunday => Array (
[SessionID] => 1
[SessionDetails] => 1:00 PM
[Description] => Foo
)
Monday=> Array (
[SessionID] => 2
[SessionDetails] => 10:00 AM
[Description] => Foo
[SessionID] => 3
[SessionDetails] => 7:00 PM
[Description] => Bar
)
I am setting a blank array for each day before my foreach ($sqlresults as $row) loop, like $sunday=array(); $monday=array(); etc.
Then inside the loop I am doing:
if (!in_array($row['SessionID'],$sunday)){
$sunday[]=array($row['SessionID'],$row['Description'], $row['SessionDetails']);
}
It sort-of-works but it's not checking the if (!in_array) part right and it's adding every single (duplicate) SessionID as a new array e.g.,
print_r($sunday)
Array
(
[0] => Array
(
[0] => 1
[1] => Foo
[2] => 1 PM
)
[1] => Array
(
[0] => 1
[1] => Foo
[2] => 1 PM
)
[2] => Array
(
[0] => 1
[1] => Foo
[2] => 1 PM
)
[3] => Array
(
[0] => 1
[1] => Barr
[2] => 1 PM
)
[4] => Array
(
[0] => 2
[1] => Barr
[2] => 10 AM
)
etc...
I tried to check the sessionID inside the array before adding to it:
if (!in_array($row['SessionID'][],$sunday)){
But "Fatal error: Cannot use [] for reading"
So I tried:
if (!in_array($sunday[$row['SessionID']],$sunday)){
But "Notice: Undefined offset: 1"
I tried setting it to associative values:
if (!in_array($row['SessionID'],$sunday)){
$sunday[]=array("SessionID"=>($row['SessionID']),"Description"=>($row['Description']), "Time"=>($row['SessionDetails']));
}
Same problem, except the array is easier to read:
[0] => Array
(
[SessionID] => 1
[Description] => Foo
[Time] => 1 PM
)
[1] => Array
(
[SessionID] => 1
[Description] => Foo
[Time] => 1 PM
)
TLDR: How can I get it to check the index [SessionID] before trying to add to the day array?
Here's a simple way to do it using array_unique.
$result = [];
foreach (array_unique($rows, SORT_REGULAR) as $row) {
$result[$row['DayofWeek']][] = $row;
}
You can run the code here.
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 7 years ago.
Array ( [0] => stdClass Object ( [Id] => 18
[AccNo] => 1
[Title] => Hardware
[Description] => Mobile.
[ManuDate] => 8th July 1942
[MusCat] => Album
[month] => 7
[date] => 8 )
[1] => stdClass Object ( [Id] => 20
[AccNo] => 2
[Title] => Food
[Description] => Apple.
[ManuDate] => 27th July 1942
[MusCat] => Album
[month] => 7
[date] => 27 )
[2] => stdClass Object ( [Id] => 24
[AccNo] => 3
[Title] => Hardware
[Description] => Computer.
[ManuDate] => 2nd July 1942
[MusCat] => Album
[month] => 7
[date] => 2 )
[3] => stdClass Object ( [Id] => 56
[AccNo] => 4
[Title] => Hardware
[Description] => Printer
[ManuDate] => 1942
[MusCat] => Album
[month] =>
[date] => 0 )
[4] => stdClass Object ( [Id] => 105
[AccNo] => 5
[Title] => Object
[Description] => Chair.
[ManuDate] => 1942
[MusCat] => Album
[month] =>
[date] => 0 ) )
This is my array input Like
Id Date Title Description
0 8th July 1942 Hardware Mobile
1 27th August 1942 Food Apple
2 2nd July 1942 Hardware Computer
3 1942 Hardware Printer
4 1942 Object Chair
I want output like
Id Date Title Description
************************************************************
3 1942 Hardware Printer
4 1942 Object Chair
2 2nd July 1942 Hardware Computer
0 8th July 1942 Hardware Mobile
1 27th August 1942 Food Apple
How to sort multikey in php?
I am beginner in Php. I am using following code in php but out put will not correctly. If any one of datewise or monthwise sort, Output will come correctly. otherwise both (datewise or monthwise), Output will not come correctly. Plz help any solution.
usort($value['year'], function ($a, $b) {
if ($a->date == $b->date) return 0;
return $a->date < $b->date ? -1 : 1;
});
usort($value['year'], function ($a, $b) {
if ($a->month == $b->month) return 0;
return $a->month < $b->month ? -1 : 1;
});
The following code does the job. strtotime parses text date into a Unix timestamp.
usort($array, function($v1, $v2) {
$d1 = strlen($v1->ManuDate) === 4 ? '01-01-' . $v1->ManuDate : $v1->ManuDate;
$d2 = strlen($v2->ManuDate) === 4 ? '01-01-' . $v2->ManuDate : $v2->ManuDate;
return strtotime($d1) - strtotime($d2);
});
My Current PHP Array Outputs like this:
Array
(
[0] => Array
(
[year] => 2015
[month] => November
)
[1] => Array
(
[year] => 2015
[month] => October
)
[2] => Array
(
[year] => 2015
[month] => September
)
[3] => Array
(
[year] => 2015
[month] => August
)
[4] => Array
(
[year] => 2015
[month] => July
)
[5] => Array
(
[year] => 2015
[month] => June
)
[6] => Array
(
[year] => 2015
[month] => May
)
[7] => Array
(
[year] => 2015
[month] => April
)
[8] => Array
(
[year] => 2015
[month] => March
)
[9] => Array
(
[year] => 2015
[month] => February
)
[10] => Array
(
[year] => 2015
[month] => January
)
[11] => Array
(
[year] => 2014
[month] => December
)
[12] => Array
(
[year] => 2014
[month] => November
)
[13] => Array
(
[year] => 2014
[month] => October
)
[14] => Array
(
[year] => 2014
[month] => September
)
[15] => Array
(
[year] => 2014
[month] => August
)
[16] => Array
(
[year] => 2014
[month] => July
)
)
I want to sort the above Array and group them based on the Year. The Array I want to convert the above is to get like this:
Array (
[0] => Array
(
[yearGroup] => “2015”
[dates] => Array
(
[0] => Array
(
[year] => 2015
[month] => November
)
[1] => Array
(
[year] => 2015
[month] => October
)
…etc..etc…
)
)
[1] => Array
(
[yearGroup] => 2014
[dates] => Array
(
[0] => Array
(
[year] => 2014
[month] => December
)
[1] => Array
(
[year] => 2014
[month] => November
)
…etc..etc…
)
)
)
This is the code I have tried so far:
$calendar = // ..... The Array Above .....;
$sidebarCalendar = array();
$currentYear = '';
$i = 0;
foreach ($calendar as $date) {
if ($currentYear != $date['year']) {
$currentYear = $date['year'];
$i++;
$tempYearDates = array();
$j = 0;
$tempYearDates[$j] = array(
'year' => $date['year'],
'month' => $date['month']
);
$j++;
// Add to Array
$sidebarCalendar[$i] = array (
'yearGroup' => $date['year'],
'dates' => $tempYearDates
);
}
else {
$tempYearDates = array(
'year' => $date['year'],
'month' => $date['month']
);
// Locate the Array Tree
$currentParent = $sidebarCalendar[$i];
$currentArray = $currentParent['dates'];
$currentArray[$j] = $tempYearDates;
$j++;
}
}
But the above outputs incomplete array like this:
Array
(
[1] => Array
(
[yearGroup] => 2015
[dates] => Array
(
[0] => Array
(
[year] => 2015
[month] => November
)
)
)
[2] => Array
(
[yearGroup] => 2014
[dates] => Array
(
[0] => Array
(
[year] => 2014
[month] => December
)
)
)
)
Can someone point out what I have got wrong here please? If there is a better way to do the same instead of the above method, please do guide me there please…
You could use some of the builtin PHP's array functions to solve this issue. First thing you want to do is get all the unique years in that array, so you could do:
$years = array_unique(array_map(function($item) { return $item['year']; }, $calendar));
Here, I've used array_map to get an array containing only the year property for each item, and fed it to the array_unique function to get only unique values.
Next thing you could do is iterate through all the years, and populate another array, something like this:
$grouped = [];
foreach($years as $year) {
$grouped[$year] = array_filter($calendar, function($item) use ($year) { return $item['year'] === $year; });
}
This way you'll get a key for each year in the grouped variable, and each key will contain a subarray composed only of the elements of calendar satisfying the array_filter condition.
I'm sure there are better / more optimized ways of doing this (maybe using array_walk or array_reduce?) and I encourage you to find those! Still, remember that there are many array functions builtin in PHP, read their documentation and learn when to use them, they'll be of great help!
Edit: I realize that the output of my solution is not exactly what you're expecting, but it's very easily adaptable. I myself prefer using keyed arrays (maps?) whenever possible in place of regular arrays with properties, but to each their own!
$grouped = [];
foreach($years as $year) {
$grouped[] = [
'yearGroup' => $year,
'dates' => array_filter($calendar, function($item) use ($year) { return $item['year'] === $year; })
];
}
Edit 2: Sometimes I forget how to PHP.