I have an array that contains an item's ID and it's quantity. The quantity is basically how many times an item has been placed in the collection. So item 1 is in the collection ten times, item 2 is in the collection two times, and item 9 is one time.
So this means that I have one full collection, because item 9 is only in there once.
$data = [
1 => 10,
2 => 2,
3 => 2,
4 => 2,
5 => 2,
6 => 2,
7 => 2,
8 => 2,
9 => 1
];
Below: I now have two collections, because item 9 is in twice.
$data = [
1 => 10,
2 => 2,
3 => 2,
4 => 2,
5 => 2,
6 => 2,
7 => 2,
8 => 2,
9 => 2
];
In the next instance I still only have two collections, because item 3 is only in there two times. hopefully it's making sense what I'm trying to do.
$data = [
1 => 10,
2 => 5,
3 => 2,
4 => 5,
5 => 8,
6 => 4,
7 => 4,
8 => 5,
9 => 6
];
The array isn't fixed to 9 items either, it can be any amount.
This should be fairly simple, but I've been racking my brain over this for quite some time, and it's just stuck; I'm too close to the problem now. I originally thought I could divide the total number of items by the quantity but that didn't work.
How do I do this so that I can loop through the array and see how many full collections I have?
Based on how you've described the issue, couldn't we simply get the lowest number in the array? Example:
$min = min($data);
In the event of a null/empty value, min() should return 0.
Related
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);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So I have an array like this:
[
543 => 1,
22 => 3,
65 => 4,
10 => 5,
50 => 6,
]
Now I get a key and a value as input. For example 22 as key and 5 as value.
Now I want to use those two inputs as start and end point in my array and want to shift all keys one forward between those two positions.
[
543 => 1,
22 => 3, ─┐ ┌─ 65 => 3,
65 => 4, ├ Shift all those keys one forward to: ┤ 10 => 4,
10 => 5, ─┘ └─ 22 => 5,
50 => 6,
]
So the expected output would be:
[
543 => 1,
65 => 3,
10 => 4,
22=> 5,
50 => 6,
]
Figure out the start and end offset from your inputs in your array:
$startIndex = array_search(22, array_keys($arr));
$endIndex = array_search(5 , array_values($arr));
//↑ Your input
So for your example array this would look like this:
[
543 => 1, //Offset: 0
22 => 3, //Offset: 1 ← 22 found; offset: 1
65 => 4, //Offset: 2
10 => 5, //Offset: 3 ← 5 found; offset: 3
50 => 6, //Offset: 4
]
Split your array into three parts:
$before = array_slice($arr, 0, $startIndex, true);
$data = array_slice($arr, $startIndex, ($endIndex - $startIndex) + 1, true);
$after = array_slice($arr, $endIndex, null, true);
Visualized this would look like this:
[
543 => 1, → $before; Where you do NOT want to shift your keys
22 => 3, ┐
65 => 4, ├ $data; Where you want to shift your leys
10 => 5, ┘
50 => 6, → $after; Where you do NOT want to shift your keys
]
Rotate the data part keys, just by merging the last key at the start with the other keys at the end:
$keys = array_keys($data);
$keys = array_merge(array_slice($keys, -1), array_slice($keys, 0, -1));
$data = array_combine($keys, $data);
Put it all back together:
$arr = $before + $data + $after;
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.
I need to extract the pricing data from a form. The form contain several dropdown options. All combo of options must be extracted.
Example of the form dropdown:
size => 1, 2, 3, 4, 5
type => 1, 2, 3, 4
color => 1, 2, 3
units => 1, 2, 3, 4, 5, 6, 7
So this particular product has 420 possible configurations size * type * color * units
How do I write some some sort of loop which will get all possible combinations?
TAG POS=1 TYPE=SELECT FORM=ACTION:/food/getPrice.do ATTR=NAME:size CONTENT=#1
By the way trying to use imacros to select an option by index as shown in the example above does not work. I am forced to select by specifying the value (such as: %1)
I also tried SET !DATASOURCE and imported a CSV file but it didn't loop through as I need it to.
--Just to clarify -- I need this kind of output (doesn't have to be in this order, but MUST output all possible combinations):
size => 1 type => 1 color => 1 units => 1
size => 1 type => 2 color => 1 units => 1
size => 1 type => 2 color => 2 units => 1
size => 1 type => 2 color => 2 units => 2
size => 1 type => 2 color => 2 units => 2
size => 1 type => 3 color => 1 units => 1
. . .
--Side Note--
If you know of how I can run this imacro from a shared server (or EC2) please advise. Thanks for the help! :)
You're looking for the cartesian product, which is described here in PHP: PHP 2D Array output all combinations
Well I am trying to sort some data in PHP. Here is an example of the array(the first column representing ids I want to still be associated with their respective values in each row):
0 1 2.0
1 15 20.0
2 15 5.5
3 15 55.1
4 2 22.3
5 20 70.8
6 2 8.2
First I would like to arrange the rows in an order where the values in the second column are in decending order:
5 20 70.8
1 15 20.0
2 15 5.5
3 15 55.1
4 2 22.3
6 2 8.2
0 1 2.0
Then, keeping those with the second column still in its arrangement, by each set of rows with the same value in the second column, arrange the third values in ascending order:
5 20 70.8
2 15 5.5
1 15 20.0
3 15 55.1
6 2 8.2
4 2 22.3
0 1 2.0
I have tried some things with the array sorting functions for PHP, however I can't figure out how to do both those operations while still maintaining row association.
One way is to use the usort function to create a custom sorting routine. There is quite a large number of ways to sort arrays in PHP depending on how you want the data sorted, multi-dimensionality, key sorts, etc. This snippet sorts based on the input and output requirements stated above.
$vals = array(
array('id' => 0, 'val1' => 1, 'val2' => 2.0),
array('id' => 1, 'val1' => 15, 'val2' => 20.0),
array('id' => 2, 'val1' => 15, 'val2' => 5.5),
array('id' => 3, 'val1' => 15, 'val2' => 55.1),
array('id' => 4, 'val1' => 2, 'val2' => 22.3),
array('id' => 5, 'val1' => 20, 'val2' => 70.8),
array('id' => 6, 'val1' => 2, 'val2' => 8.2)
);
usort($vals, 'sortfn');
function sortfn($a, $b)
{
if($a['val1'] == $b['val1'])
return ($a['val2'] > $b['val2']);
else
return ($a['val1'] < $b['val1']);
}
var_dump($vals);