laravel sorting arrays last 5 unique and reverse - php

I have an array of id's and I want to filter those id's to last 5 and unique ids.
$recently_viewed_ids
array:16 [▼
0 => 1
1 => 2
2 => 1
3 => 2
4 => 8
5 => 7
6 => 6
7 => 6
8 => 6
9 => 5
10 => 8
11 => 4
12 => 1
13 => 1
14 => 1
15 => 1
]
Here is my code and it's messing up because I'm getting 85672
$items = array_slice(array_unique(array_reverse($recently_viewed_ids)), -5);
Output I am expecting
14856

You need to use next combination of array functions:
array_slice( // get first 5 values
array_unique( // get only unique values
array_reverse($arr) //reverse array for get last values
)
,0,5);
Code example here: PHPize.online

In Laravel, I believe you can rewrite the correct answer to:
return collect($arr)
->reverse()
->unique()
->slice(0, 5)
->all();

try this :
$items = array_slice(array_unique(array_reverse($recently_viewed_ids)), 5);

Related

Sort an array of numbers into two arrays of equal length in PHP

I have an array of 20 different numbers which contains ranking of students.
I want to split this array into two sub arrays of equal length i.e 10
I also want the sum of all numbers within each array to be close.
For example, in sub-array A there could be a total sum of 56 and in sub-array B there could be a total sum of 57.
I am using PHP.
I sort the main array here and would like to assign index[0] to sub-array A and index[1] to sub-array B, and keep repeating this until both arrays are filled.
My approach works but i think its not great and not dynamic.
I interate through the main original array for [i] and then add that to the first sub-array, then I set i = i+2 so that I get every second value and store them in the first array.
I then remove the value at index[i] from the main array.
What is left over is now sub-array B.
$kids = array (8,5,6,9,3,8,2,4,6,10,8,5,6,1,7,10,5,3,7,6);
sort($kids);
$arrlength = count($kids);
for($x = 0; $x < $arrlength; $x++) {
echo $kids[$x];
echo "<br>";
}
$teamA = array();
$teamB = array();
$i = 0;
while ($i < $arrlength)
{
#echo $kids[$i] ."<br />";
array_push($teamA, $kids[$i]);
unset($kids[$i]);
$i += 2;
}
$teamB = $kids;
print_r($teamA);
print_r($teamB);
My Output is :
Array ( [0] => 1 [1] => 3 [2] => 4 [3] => 5 [4] => 6 [5] => 6 [6] => 7 [7] =>
8 [8] => 8 [9] => 10 )
The sum here of all values is = 58
Array ( [1] => 2 [3] => 3 [5] => 5 [7] => 5 [9] => 6
[11] => 6 [13] => 7 [15] => 8 [17] => 9 [19] => 10 )
The sum here of all values is = 61
Any help is greatly appreciated. I have no real experience with PHP or its built in functions so sorry if this is a basic question. Thanks!
There is a built-in helper function of php, array_slice. You can read about it in the link I provided.
Here's how you can use in to achieve what you want:
$kids = array (5,7,6,8,3,8,2,4,6,10,8,5,6,10,7,6,5,3,7,6);
sort($kids);
$arrlength = count($kids);
$arrayA= array_slice($kids, 0, $arrlength / 2);
$arrayB= array_slice($kids, $arrlength / 2);
Output
// $arrayA
array:10 [▼
0 => 2
1 => 3
2 => 3
3 => 4
4 => 5
5 => 5
6 => 5
7 => 6
8 => 6
9 => 6
]
// $arrayB
array:10 [▼
0 => 6
1 => 6
2 => 7
3 => 7
4 => 7
5 => 8
6 => 8
7 => 8
8 => 10
9 => 10
]
Another approach for achieving what you asked
$kids = array(8,5,6,9,3,8,2,4,6,10,8,5,6,1,7,10,5,3,7,6);
sort($kids);
$teamA = array();
$teamB = array();
foreach($kids as $i => $kid){
if($i % 2){
array_push($teamA, $kid);
} else{
array_push($teamB, $kid);
}
}
It will generate the same output and sum as you want.

how to WhereIn a whereday of array in laravel

array:23 [▼
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
5 => 8
6 => 9
7 => 10
8 => 11
9 => 12
10 => 15
11 => 16
12 => 17
13 => 18
14 => 19
15 => 22
16 => 23
17 => 24
18 => 25
19 => 26
20 => 29
21 => 30
22 => 31
]
this is a array of working days apart from Sunday and Saturday and i have a table of months data and i need a Laravel where condition for comparing all data with whe
->whereYear('Clock_Day',$yearofdata)
->whereMonth('Clock_Day',$monthofdata)
->whereIn('Clock_Day','=',$workdays) //here can i use something like whereIn->whereday---for comparing all array values and get as per the data
There is no whereDayIn() or similar method, but you can do this:
->whereYear('Clock_Day', $yearofdata)
->whereMonth('Clock_Day', $monthofdata)
->where(function($q) use($workdays) {
foreach ($workdays as $day) {
$q->whereDay('Clock_Day', '=', $day, 'or');
}
})
If Clock_Day is a field in your table, you will need to extract the date part you are comparing for each piece (Year, Month, Day). You could possibly use whereIn with DB::raw:
->where(DB::raw('YEAR("Clock_Day")'),$yearofdata)
->where(DB::raw('MONTH("Clock_Day")'),$monthofdata)
->whereIn(DB::raw('DAYOFMONTH("Clock_Day")'),$workdays)
You would only use whereYear or whereMonth to compare those values against fields in your table called 'year' and 'month', but since you want to use the same 'Clock_Day' field for all comparisons you need to extract the relevant data for each part.

Subtracting many arrays

I have one big array circa 50 values like this (for example I used smaller array):
Array
(
[Adam Małysz] => 1
[Justyna Kowalczyk] => 2
[Janne Ahonen] => 3
[Stefan Hula] => 4
[Ole Einar Bjoerdalen] => 5
[Jakub Janda] => 6
[Mariusz Pudzianowski] => 7
[Harry Potter] => 8
[Vladimir Zografski] => 9
[Pavel Karelin] => 10
[Eddie Edwards] => 11
[Apoloniusz Tajner] => 12
)
and I have n smaller constant arrays with different values:
Array
(
[Adam Małysz] => 1
[Janne Ahonen] => 2
[Stefan Hula] => 3
[Ole Einar Bjoerdalen] => 4
[Vladimir Zografski] => 5
[Pavel Karelin] => 6
[Apoloniusz Tajner] => 7
[Mariusz Pudzianowski] => 8
[Jakub Janda] => 9
[Harry Potter] => 10
)
Array
(
[Justyna Kowalczyk] => 1
[Apoloniusz Tajner] => 2
[Harry Potter] => 3
[Janne Ahonen] => 4
[Mariusz Pudzianowski] => 5
[Adam Małysz] => 6
[Jakub Janda] => 7
[Ole Einar Bjoerdalen] => 8
[Vladimir Zografski] => 9
[Pavel Karelin] => 10
)
Array
(
[Adam Małysz] => 1
[Janne Ahonen] => 2
[Jakub Janda] => 3
[Stefan Hula] => 4
[Ole Einar Bjoerdalen] => 5
[Justyna Kowalczyk] => 6
[Harry Potter] => 7
[Mariusz Pudzianowski] => 8
[Vladimir Zografski] => 9
[Apoloniusz Tajner] => 10
)
So I want to subtract array values where is the same key like:
Big Array: [Harry Potter] => 8
[Mariusz Pudzianowski] => 7
Small Array #1: [Harry Potter] => 10
[Mariusz Pudzianowski] => 8
Small Array #2: [Harry Potter] => 3
[Mariusz Pudzianowski] => 5
Output: Difference between Small Array #1 and Big Array for key [Harry
Potter] = 2
Difference between Small Array #1 and Big Array for key [Mariusz Pudzianowski] = 1
Output: Difference between Small Array #2
and Big Array for key [Harry Potter] = 5
Difference between Small Array #2 and Big Array for key [Mariusz Pudzianowski] = 2
Basically I'm stuck.
Please help.
lets name your big array as $bigArray and small array as $smallArray both having key-value pair. Now the code should be:
foreach($bigArray as $bigKey=>$bigValue){
foreach($smallArray as $smallKey=>$smallValue){
if($smallKey==$bigKey){
$output=$bigValue-$smallValue;
echo 'Difference between Small Array #1 and Big Array for $bigKey is '.$output;
}
}
}
This way for every element of your big Array , you will loop small array and check if the keys are matching and if they do then you subtract their values and print them. You can do this for other small arrays as well.
You could use array_intersect_key to find all the key that are common to all arrays.
$commonKeys = array_intersect_key($bigArray, $smallArray1, $smallArray2, ...)
From there you just have to iterate over $commonKeys and make the appropriate substractions. Something like :
foreach ($commonKeys as $key) {
$result = abs($bigArray[$key] - $smallArray1[$key]);
echo " Difference between Small Array #1 and Big Array for key [$key] = $result";
//you do that for each Small Array
}

Merging 2 arrays by keys

There are 2 arrays, the reason for 2 arrays is because of 2 drop down from form post.
array1
1 => 878
2 => 983
3 => 717
array2
1 => 10
2 => 15
3 => 12
I have to loop 2 arrays and get where the keys matched and combine them into an array.
878 => 10
983 => 15
717 => 12
Thanks!
using the array_combine. try it here
$combined = array_combine(array_values($array1),array_values($array2));

Sorting with arsort not stable [duplicate]

This question already has answers here:
How to have a stable sort in PHP with arsort()?
(7 answers)
Closed 9 years ago.
There is strange issue in php asort, arsort.
I am taking example of arsort
Case1
$a = array(
1 => 2,
2 => 1,
3 => 2,
4 => 1
);
arsort($a);
var_dump($a);
Output:
array(4) {
[3] =>
int(2)
[1] =>
int(2)
[4] =>
int(1)
[2] =>
int(1)
}
Here at index (3,1) and (4,2) are sorted in descending order because at index 3 and 1 values are same. Same for index 4 and 2.
Case2
$a = array(
1 => 2,
2 => 1,
3 => 2
);
arsort($a);
var_dump($a);
Output:
array(3) {
[1] =>
int(2)
[3] =>
int(2)
[2] =>
int(1)
}
Here at index (3,1) are sorted in ascending order and still at index 3 and 1 values are same.
Is there any solution for this issue? As I want that ordering should be certain. Like sort in either descending or ascending order if value at some indices are same.
According to the PHP documentation:
If any of these sort functions evaluates two members as equal then the order is undefined (the sorting is not stable).
You can't know exactly which behaviour is correct, with testing just with 2 elements. Here's an array with multiple elements (odd and even).
even number:
<?php
$a = array(
1 => 2,
2 => 1,
3 => 2,
4 => 1,
5 => 2,
6 => 1,
7 => 2,
8 => 1,
9 => 2,
10 => 1,
11 => 2,
12 => 1
);
arsort($a);
var_dump($a);
Result:
array (size=12)
1 => int 2
7 => int 2
5 => int 2
11 => int 2
9 => int 2
3 => int 2
10 => int 1
12 => int 1
6 => int 1
2 => int 1
4 => int 1
8 => int 1
odd number
<?php
$a = array(
1 => 2,
2 => 1,
3 => 2,
4 => 1,
5 => 2,
6 => 1,
7 => 2,
8 => 1,
9 => 2,
10 => 1,
11 => 2,
12 => 1,
13 => 2
);
arsort($a);
var_dump($a);
Result
array (size=13)
9 => int 2
11 => int 2
13 => int 2
1 => int 2
7 => int 2
3 => int 2
5 => int 2
12 => int 1
2 => int 1
4 => int 1
8 => int 1
6 => int 1
10 => int 1
The question is now, where did it add the 13th element (=2)? it's added just after the 11th element and before the first element...this means that there's no rule here. (At least according to what we see).
We can't say that it follows any rule with testing with just 2 variables like what you did, because you saw (1,3) and you presumed that it's sorted by key. Which is apparently not the case with multiple variables.

Categories