I am using laravel 5 and having following array:
array:3 [▼
0 => 3,
1 => 4,
2 => 5
]
Now i wanted to get all values/rows from table say 'X' having id's 3,4,5
Try this query
$array = [ 0 => 3,1 => 4, 2 => 5];
$results = DB::table('x')
->whereIn('id',$array)
->get();
You can see the query sample for Laravel 5
$result=DB::table('x')->whereIn('id',[3,4,5])->get();
Related
I would like to count two columns in a table and group them in one query. My table:
id
is_correct
question_id
answer_id
1
0
18
67
2
0
18
67
4
1
18
68
2
0
18
67
My queries look like this:
$questions_ids = array(1, 5, 8, 10, 18, 20);
$count_total_answers = $this->whereIn('question_id', $questions_ids)
->select('question_answer_id', DB::raw('count(*) as count_answer_id'))
->addSelect('question_id')
->groupBy(['question_answer_id'])
->get()->toArray();
$count_total_questions = $this->whereIn('question_id', $questions_ids)
->select('question_id', DB::raw('count(*) as count_question_id'))
->groupBy('question_id')
->get()->toArray();
result:
result
I would like to obtain such an array:
array:2 [▼
0 => array:3 [▼
"question_answer_id" => 67
"count_answer_id" => 3
"count_question_id" => 4
]
1 => array:3 [▼
"question_answer_id" => 68
"count_answer_id" => 1
"count_question_id" => 4
]
]
Is this possible in a single query?
Greetings :)
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);
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.
I have a multidimensional array $elements where I need to fill it with values from the array $ratings. The array $ratings is built so the first value will fit into the first slot in elements, the next in the second and so on.
$elements
4 => array:3 [▼
2 => 0
3 => 0
4 => 0
]
5 => array:3 [▼
2 => 0
3 => 0
4 => 0
]
7 => array:3 [▼
2 => 0
3 => 0
4 => 0
]
I now need to fill $elements with 9 specific values from
$ratings
array:9 [▼
0 => 3
1 => 2
2 => 1
3 => 3
4 => 3
5 => 2
6 => 3
7 => 2
8 => 1
9 => 3
]
If I manage to loop through $elements, inserting values from $ratings one by one, I will have solved my problem.
So $elements[4][2] should have the value of 3, $elements[4][3] should have value of 2 etc.
Also you can manipulate these by array_fill using loop.
Try this:
<?php
$elements = [
4=>[2=>0, 3=>0, 4=>0],
5=>[2=>0, 3=>0, 4=>0],
7=>[2=>0, 3=>0, 4=>0],
];
$ratings = [ 0 => 3, 1 => 2, 2 => 1, 3 => 3, 4 => 3, 5 => 2, 6 => 3, 7 => 2, 8 => 1, 9 => 3 ];
$ratingsIndex = 0;
foreach(array_keys($elements) as $ElementsIndex) {
foreach(array_keys($elements[$ElementsIndex]) as $ElementsSubIndex) {
$elements[$ElementsIndex][$ElementsSubIndex] = $ratings[$ratingsIndex++];
}
}
echo "<pre>";
print_r($elements);
echo "</pre>";
?>
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);