This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Find common values in multiple arrays with PHP
I'm trying to find the number of friends two users have in common. Each users friends user id's are stored in the data base like this: 12, 13, 14. This is my code.
$my_friends = explode(',', $my_friends);
print_r($my_friends);
This outputs: Array ( [0] => 12 [1] => 13 [2] => 14 )
These are my friends user id's. Now for the next user:
$users_friends = explode(',', $users_friends);
print_r($users_friends);
This outputs: Array ( [0] => 12 )
How do I show that user 1 and user 2 have id 12 in common ?
Besides suggesting that you should normalise your database, which would make this easy to do with a simple SQL Query:
Explode the $myFrieds and $users_friends arrays, then use the array_intersect() function to find the common entries, and count() those
You could use array_intersect().
print_r(array_intersect($my_friends, $users_friends));
will output Array ( [0] => 12 )
You can use array_intersect:
$common_friends = array_intersect($my_friends, $users_friends);
Related
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 1 year ago.
Consider this array:
Array( [Wingspan] => 5
[Scythe] => 1
[Spirit Island] => 2
[Everdell] => 1)
How can I sort this array with the highest value(5) as first, then 2 then lowest (1) as the last? I used this:
print_r(rsort($_POST['item']), SORT_NUMERIC);
But I get this:
Array
(
[0] => 2
[1] => 1
[2] => 1
[3] => 1
)
It changes the key, but this is wrong. I expect to get this:
Array( [Wingspan] => 5
[Spirit Island] => 2
[Scythe] => 1
[Everdell] => 1)
I searched and found this: Sort an Array by numerical value but that did not help.
Use the method
arsort($_POST['item'])
instead of rsort. rsort only works for simple arrays, not for associative ones. But anyways usually if the order of te items matters to you, probably an associative array is not the best choice. You could use a simple array with objects inside containing the values and the keys at once
This question already has answers here:
How to combine two arrays together?
(3 answers)
Closed 6 years ago.
I am having two arrays like this
Array
(
[0] => username
[1] => avatar
)
Array
(
[0] => name
[1] => 4.jpg
)
Here, I need to get these values in the following format
'username'=>name,'avatar'=>4.jpg
i.e., merge the same key values in the above format..
How should I do this,..Someone help me..
If you think that my title is wrong,Please change it into correct format.
Thanks,
Use array_combine()
$final_array = array_combine($fist_array,$second_array);
Reference:- http://php.net/manual/en/function.array-combine.php
use $c = array_combine($a, $b);
This question already has answers here:
Get first key in a (possibly) associative array?
(25 answers)
Closed 5 years ago.
hi i have a multidimensional l array .
Array
(
[1] => Array
(
[38] => Fashion Retail | Fashion Accessories
)
[10] => Array
(
[194] => Automotive | 4x4
[206] => Automotive | Aftermarket Parts and Kits
[201] => Automotive | ATVs
)
)
i want to get the first sub array's key , in this scenario it is 1 , i can get it using a foreach loop .
foreach($myarry as $key=>$val)
is there any way to achieve this with out looping , please help . thanks in advance
If using >= PHP 5.5...
$first = array_keys($myarry)[0];
If using an older PHP, just assign the keys somewhere, and then subscript the first element as normal.
$arrKeys = array_keys($array);
$key = array_shift($arrKeys); // gives first key
Returns the first key and deletes this from the arrKeys, hence the next key, 10 in this case will be returned on next call. No need to make another array.
yep, I did that , I have used
current(array_keys($my_array))
print_r($myarry[array_keys($myarry)[0]]);
Please try:
reset($myarry);
$first_key = key($myarry);
This question already has answers here:
PDO fetchAll array to one dimensional
(3 answers)
Closed 3 years ago.
This seems like it should be (and probably is) trivial. I have a simple query:
SELECT Name From User;
When I run the query using this code:
$rows = $preparedStatement->fetchAll(PDO::FETCH_ASSOC);
$Rows looks like this:
Array ( [0] => Array ( [Name] => Doug ) [1] => Array ( [Name] => John ) )
Is there an easy way to make the array look something like this:
Array( Doug, John)
Using the constant PDO::FETCH_COLUMN:
$columnNumber = 0;
$rows = $preparedStatement->fetchAll(PDO::FETCH_COLUMN, $columnNumber);
This way you'll get exactly what you proposed.
You can also do the following:
$columnNumber = 0;
$rows = $preparedStatement->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE, $columnNumber);
This way you'll get an array with unique values.
Source: http://www.php.net/manual/en/pdostatement.fetchall.php
I think the right answer has been given by Jaison Erick, in case you need to flatten what you've got returned (not really recommended), this would do it:
$flat = reset((call_user_func_array('array_merge_recursive', $rows)));
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
PHP - sort an array based on another array?
Need some help regarding array sorting....
I have two arrays. The main one (where the key is the user id) :
$user[31] = 'Tom'
$user[43] = 'Jane'
and another array with the order they should be displayed (where key is the order and value is the user id) :
$order[1] = 43
$order[2] = 31
How can I apply the ordering to the main array using the ordering one?
Thanks guys!
Use the keys in $order to select the users from $user in the right order:
$orderedUsers = array();
foreach ($order as $key) {
$orderedUsers[] = $user[$key];
}
Use this one, it is usefull for your problem
$array = array("size" => "XL", "color" => "gold");
print_r(array_values($array));
Result as :
Array
(
[0] => XL
[1] => gold
)