I have 2 arrays one retrieved from a database (saved results) and the other from an xml (new results)
$fromDB = array('123','124','524','15','616');
$fromXML = array('123','124','524','15','818');
I want to compare those two and see which values are old (fromDB) and which are new (fromXML) so to insert the old value in a different table.
How can i achieve this?
The array_diff function is what you're looking for.
Take a look at the array_diff() function
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);
Database table SITE has many columns. One of them is site_id. I need all the site_ids as an array since it has to be fed to a method which accepts only a string array.
What I tried so far is:
$sites = DB::select('select site_id from site_tab');
$sites_arr = $sites->toArray();
But this doesn't produce the result I want. I need $sites_arr to be like ['A','B','C',...]
Please suggest a way to get this done. A solution based on Eloquent is also OK for me.
Thanks
Try this:
DB::table('site_tab')->pluck('site_id')->toArray();
reference pluck
referen toArray
If you open a manual, you will see that
The select method will always return an array of results
So, there's no need to use ->toArray(), as result is already an array.
To get values as array of names you can do:
$site_ids = DB::table('site_tab')->pluck('site_id');
Using ->toArray() here is optional, as you can iterate over $site_ids (which is a Collection) with a foreach too.
i'm relatively new to coding and I need a little help. I'm basically trying to loop through an entry in a mySQL database and push any new entry into an array , so that it only comes up once in my array.
// SQL query
$response = $bdd->query('SELECT serie_bd FROM inventaire_bd');
//creating array to group all elements from the db so that they do not repeat
$serie_bd_groupe=array();
while($data_collected = $response->fetch())
{
if(array_key_exists($data_collected,$serie_bd_groupe)==false)
{
array_push($data_collected,$serie_bd_groupe);
}
}
Will this work? - it seems like the loop will just stay stuck after it comes accross an entry a second time because the if statement wont execute itself.
Also in the future, are their any php equivalent to jsfiddle.net so i can test code syntaxically?
Thank you for your time
Your array keys will be default integers, so you don't want to check those. Instead of this:
if(array_key_exists($data_collected,$serie_bd_groupe)==false)
you should do this:
if(!(in_array($data_collected,$serie_bd_groupe)))
http://php.net/manual/en/function.in-array.php
On the other hand, if you're expecting your collected data to be the array key rather than value, you'd do something like this, instead of your array_push:
$serie_bd_groupe[$data_collected] = 1;
then your key check would work.
If you are looking for UNIQUE values (serie_bd) from your database, update your query to include "DISTINCT" like this:
$bdd->query('SELECT DISTINCT serie_bd FROM inventaire_bd');
On the other hand, I think you are looking for http://phpfiddle.org/
I am trying to insert values from a multi select drop down in a form to a mysql db column. For example: The drop down would have one or more choices chosen then when the form is posted it would insert the data into one column in a mysql db. I'm stuck at how to insert the data.
If you want to insert in single row then you can use implode() to generate comma separated data, or you can do json_encode() and add to your colum.
Say you get the data as
$data = array("one", "two", "tree");
// output one, two, three
$insert_data = implode(",", $data);
or
$insert_data = json_encode($data);
Thats for inserting data in single column. While retrieving you can do explode() or json_decode() to get the return data and can use them in the multi-select again.
If you want one row for each item then just loop through the array and add them
you can turn the array into a single string with http://us1.php.net/function.implode
$comma_separated = implode(",", $array);
set the type of the column to string, then use the function serialize($array) to convert the array into a string. When you want to get the string back to an array, use unserialize($string)
A couple of things to think about:
If there is a one to many relationship - it shouldn't be in one column, look into multiple tables and changing your database structure.
If you really want to pass in an array you will need to convert it to string using the php built in function implode, then using the built in function explode to retrieve the column from the db
$arr = array('val1','val2');
$string = implode(',',$arr);
//Do db insert
//Do db retrieve
$arr = explode(',',$string);
http://php.net/function.implode
I have a simple mySQL database table that I am loading into a PHP array. I would like the id column of the mySQL table (which is auto incremented, but I don't think that's relevant) to be the array key for each element of the PHP array, instead of the array being numeric.
Instead of this:
Array(
Array(id=>'1', field1=>someval, field2=>val),
Array(id=>'2', field1=>val, field2=>otherval),
Array(id=>'4', field1=>val, field2=>otherval)
)
I want this:
Array(
1=>Array(field1=>someval, field2=>val),
2=>Array(field1=>val, field2=>otherval),
4=>Array(field1=>val, field2=>otherval)
)
I don't care if id is left in the associative array for each row.
Is there a way to do this without looping through the original mySQL array and using up lots of processing time?
You can do it at the fetch time like this:
$query_ret = mysql_query(...);
$result = array();
while ($row = mysql_fetch_assoc($query_ret)) {
$result[array_shift($row)] = $row;
}
"Is there a way to do this without looping through the original mySQL array and using up lots of processing time?"
I believe the answer to this is no. The best you can do is to try to be as efficient as possible when looping through the array.
If you have PDO, you should definitely see Example #3 from the PDO documentation for fetchall: http://www.php.net/manual/en/pdostatement.fetchall.php#example-1022
Not only is this way more efficient use of your server's memory and processing power... it would also enable you to take advantage of several of PDO's other powerful APIs.