does anyone know how to loop an array & sum it? So i have an array with 3 values like in the picture below
and this is my code
use laravel collection sum() method instead of getting sum of array value in loop
collect([1, 2, 3])->sum();
or use php array_sum() method
array_sum([1, 2, 3, 4, 5]);
Related
I am having some trouble while trying to compare two plucked collections. Objective is to compare the plucked values and get those values that are not present in both arrays.
I tried the following for this
$users = message::withTrashed()->where([
'sentTo' => $authId,
'isDraft' => 0
])->groupBy('group_message_id')->pluck('group_message_id')->all();
$checkDeleted = inboxDeleted::whereIn('thread_id',$users)
->where('user_id',$authId)
->pluck('thread_id')->all();
From here same values should be eliminated and distinct values should be kept. Is it possible to compare plucked values? If no then how to check the plucked values.
Data should not be fetched from query?
Thanks for suggestions. :)
You can use diff() (Laravel solution):
$diff = $users->diff($checkDeleted);
$diff->all();
From the docs:
The diff method compares the collection against another collection or a plain PHP array based on its values. This method will return the values in the original collection that are not present in the given collection
As I know, pluck returns values of particular column as array instead of collection.
So, you can use array_diff() method like this:
$difference = array_diff($users,$checkDeleted);
This will give you desired result.
You can use
$result=array_diff($a1,$a2);
It seems that the standard pagination component in CakePHP 3 only allows the pagination of database queries. How can I adjust/overwrite pagination in order to display pagination of an array of data?
you can use Collection to manipulate array data
http://book.cakephp.org/3.0/en/core-libraries/collections.html
Cake\Collection\Collection::take(int $size, int $from)
Whenever you want to take a slice of a collection use the take() function, it will create a new collection with at most the number of values you specify in the first argument, starting from the position passed in the second argument:
$topFive = $collection->take(5);
// Take 5 data from the collection starting from position 4
$nextTopFive = $collection->sortBy('age')->take(5, 4);
Positions are zero-based, therefore the first position number is 0.
I got database table like this:
**job_id**
5
5
5
6
6
7
8
8
I want to write query, which could select only unique id's. By saying unique I mean select only these values once:
5, 6, 7, 8
Thanks in advance!
You could use DISTINCT.
DB::table('table')->select('job_id')->distinct()->get();
How about:
$jobs = DB::table('my_job_table')
->groupBy('job_id')
->get();
Eloquent:
First, you need a model. You could generate this by using php artisan. php artisan make:model jobs (I assume you have done this already) This will create a model in /your_project/app/Job.php
Now you can use Eloquent (here in a route, to see some output):
Route::get('/jobs', function () {
$jobs = \App\Job::groupBy('job_id')->get();
return $jobs->lists('job_id');
});
will return something like: [0,1,3,4] instead of [0, 1, 1, 1, 3, 4, 4, 4].
I have implemented ZendSearch into my Laravel application. I am using it as my search engine where users will type a search word, and then ZendSearch will return me an array of results ordered by relevance. However, the array that ZendSearch returns, only returns my record ID's (it doesn't return any of the actual record information).
What would next be the correct way to query my Model to retrieve the results based on the ZendSearch array results which is just an array of ID's ordered based on relevance.
I know of Model::find(1) which would return my record with an ID of 1, but how can I feed that find() method an array of ID's that I want to be returned in the order I am giving it.
That's simple. Use findMany:
$models = Model::findMany([1, 2, 3]);
By the way, you can also pass an array to find() and it will internally call findMany:
$models = Model::find([1, 2, 3]);
Under the hood it just does a whereIn so you could do that too:
$models = Model::whereIn('id', [1, 2, 3])->get();
Just use ->find($ids)
$ids = [1,2,3,4]
$model = Model::find($ids);
in my case, i use query like this
$ids = [1,2,3,4]
$model = Model::query()->find($ids);
I used that in Lumen.
i want to store my PHP values in db(mysql) in the form of array...
for example
$a=array{10,20,30,40};
i want to store this variable $a in to db in the array form like how it's storing in array using index.
why i want to do this because in future i may have to perform update or delete operation on the array values..
i know that it's possible to do this thing... but i don't know how to implement this..
i searched about this topic but i didn't get proper answer....
Please suggest me how to do this things...
Why don't use json_encode in PHP and store it on your database. It's the best way.
The array will be converted to a string and will be stored.
Retrieve the data and make use of json_decode and then start working as per your needs.
Example:
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
OUTPUT: {"a":1,"b":2,"c":3,"d":4,"e":5}
You should create a distinct TABLE to store this kind of data.
Consists of 2 columns, corresponding record ID and the actual data.
So, your record will be looks like
rid value
1 10
1 20
1 30
1 40
2 10
2 40
...
this way you will be able to perform update or delete operation on the array values using conventional SQL routines, as well as selecting data based on the array values.
This is how the things done oin the real world, not in PHP sandbox.
All othe answers here are plainly wrong
I would use serialize/unserialize for this. You can use it like this:
Send to MySQL
<?php
$a = array{10,20,30,40};
$a = serialize($a);
// your code here to send it to the mysql
?>
Get from MySQL
<?php
// your code here to collect it from mysql
$a = unserialize($mysql->str);
?>
The field in the MySQL should be TEXT or VARCHAR.
Regards
BlackBonjour
You can always serialize your array then store the result in a VARCHAR or TEXT field and after fetching you can unserialize the field.