Friends highscore: merging two arrays - php

I'm developing a game on Facebook and want to show the users a friends highscore.
What's the most efficient way to do this?
I have two arrays:
The first one, $all, coming from my database, holds the fbid's and the score's of ALL users who played the game:
Array
(
[0] => Array
(
[uid] => 12345
[score] => 0
[endgame] => 1404316845
)
[1] => Array
(
[uid] => 112873
[score] => 0
[endgame] => 1404334512
)
...
)
The second one, $friends, coming from Facebook, holds ALL the user's friends:
Array
(
[0] => Array
(
[uid] => 12345
[name] => Some Name
[pic_square] => https://...
)
[1] => Array
(
[uid] => 43324324
[name] => Another Name
[pic_square] => https://...
)
[2] => Array
(
[uid] => 4893424242
[name] => Yet Another
[pic_square] => https://...
)
...
)
I think the prefered method here is to generate an new array, which contains only friends who played the game and have a score, like this:
Array
(
[0] => Array
(
[uid] => 12345
[name] => Some Name
[pic_square] => https://...
[score] => 0
[endgame] => 1404316845
)
...
)
I've tried various things using in_array, but can't get it right... Anyone who can help me?

Create a new array for the scores that is indexed by uid.
$indexedScores = [];
foreach($all as $score) {
$indexedScores[$score['uid']] = $score['score'];
}
Then loop through the friends array and set the score by looking up $indexedScores
foreach($friends as $key => &$friend) {
if(isset($indexedScores[$friend['uid']])) {
$friend['score'] = $indexedScores[$friend['uid']];
}
else {
unset($friends[$key]); //doesn't have a score, so remove
}
}
usort($friends, function($a, $b) {
return $a['score'] > $b['score'];
});

Related

Array parsing using array value

i have below array,and i have amenities id = 50,i need to show amenities name like 'Express check-out' using amenities id = 50 from this array using php.
Array
(
[amenities] => Array
(
[0] => Array
(
[id] => 0
[name] => Cash machine
[key] => CASHMACHINE
)
[1] => Array
(
[id] => 42
[name] => Express check-in
[key] => EXPRESSCHECKINSERVICE
)
[2] => Array
(
[id] => 50
[name] => Express check-out
[key] => EXPRESSCHECKOUTSERVICE
)
[5] => Array
(
[id] => 3
[name] => Wi-Fi
[key] => WIFISERVICE
)
)
)
There are many ways that your problem can be solved. Easy way can be as follow
function getAmenities($array,$id){
foreach($array['amenities'] as $tmp_arr)
if($tmp_arr['id']==$id)
return $tmp_arr['name'];
}
echo getAmenities($array,50);
I have not checked result but should work fine. Please let me know if this works for you
How do u create this Array?
Can't u just use the id as the key when u create it, like:
$key = $array2['id'];
$array['amenities'][$key] = $array2;

Extract data fron JSON array using loop

im trying to filter a JSON array response as i only need a small part of the results.
I need to get the players displayName only.
Here is the repose for the first player, there can be upto 12 player per match.
I need something that can loop through and extract the names..
[displayName] => jonhofun
At present the only way i can get the data i need is by doing
$player1 = $json11['Response']['data']['entries']['0']['player']['destinyUserInfo']['displayName'];
$player2 = $json11['Response']['data']['entries']['1']['player']['destinyUserInfo']['displayName'];
etc... etc...
heres the original response
Array
(
[Response] => Array
(
[data] => Array
(
[period] => 2016-08-20T10:16:46Z
[activityDetails] => Array
(
[referenceId] => 3156370656
[instanceId] => 5370359303
[mode] => 12
[activityTypeHashOverride] => 3614615911
)
[entries] => Array
(
[0] => Array
(
[standing] => 0
[score] => Array
(
[basic] => Array
(
[value] => 2190
[displayValue] => 2,190
)
)
[player] => Array
(
[destinyUserInfo] => Array
(
[iconPath] => /common/destiny_content/icons/d0d3cd4c26aa1a931d46c4bf720856ba.jpg
[membershipType] => 2
[membershipId] => 4611686018454971653
[displayName] => jonhofun
)
[characterClass] => Warlock
[characterLevel] => 40
[lightLevel] => 322
)
)
)
)
)
)
Any help would be appreciated.
You need to loop through the sub array under "entries".
foreach ($json11['Response']['data']['entries'] as $entries) {
$player_names[] = $entries['player']['destinyUserInfo']['displayname'];
}
echo "<pre>";
print_r($player_names); // Check all player names

Get JSON value from PHP

All, I have got a JSON response from NEO4J:
Array
(
[columns] => Array
(
[0] => n
)
[data] => Array
(
[0] => Array
(
[0] => Array
(
[outgoing_relationships] => http://localhost:7474/db/data/node/1/relationships/out
[labels] => http://localhost:7474/db/data/node/1/labels
[data] => Array
(
[position] => Developer
[awesome] => 1
[name] => Michael
[children] => 3
)
[traverse] => http://localhost:7474/db/data/node/1/traverse/{returnType}
[all_typed_relationships] => http://localhost:7474/db/data/node/1/relationships/all/{-list|&|types}
[property] => http://localhost:7474/db/data/node/1/properties/{key}
[self] => http://localhost:7474/db/data/node/1
[properties] => http://localhost:7474/db/data/node/1/properties
[outgoing_typed_relationships] => http://localhost:7474/db/data/node/1/relationships/out/{-list|&|types}
[incoming_relationships] => http://localhost:7474/db/data/node/1/relationships/in
[extensions] => Array
(
)
[create_relationship] => http://localhost:7474/db/data/node/1/relationships
[paged_traverse] => http://localhost:7474/db/data/node/1/paged/traverse/{returnType}{?pageSize,leaseTime}
[all_relationships] => http://localhost:7474/db/data/node/1/relationships/all
[incoming_typed_relationships] => http://localhost:7474/db/data/node/1/relationships/in/{-list|&|types}
)
)
)
)
I do not know how to retrieve the value "position" under the data array, because the array is under another array.
Can you tell me how to do it with PHP?
Thx
Access them like this.
echo $yourarr['data'][0][0]['data']['position'];
Tips on how to locate :
Just locate where is the position keyword, Now look up to the array, As you can see the parent of position is data, think of how you reach from the start
(data)to the destination(position) (like a maze).
When you have more than 1 record try this
foreach($var['data'] as $inside){ //inner 1st stage
foreach($inside as $index => $main){ //inner 2nd stage
if($index == 'data'){ //check if index is data
echo $main['position']; //output position
}
}
}

Recursive function for a multidimensional array?

I am attempting to use a recursive function to search through a multidimensional array, such as the one below, to find certain values, i.e. people who went to certain school, majored in a certain subject, hold a certain job title, etc. In case your wondering, this array is output from the Facebook Graph API. In reality there are more than 3 offset arrays, depending on the number of friends a user has, it could be in the thousands.
Here's a solution I tried with very little knowledge of recursive functions (my first thought was to use in_array before I found out it didn't work for md arrays):
So to give you an idea of how the md array below works, check out this snippet of code:
$friend = $fqlResult[0]['name'];
echo "$friend";
*The output would be "BLANK" since I deleted the person's name.
$data = $fqlResult;
$collegemajor = (isset($value['education'][0]['concentration'][0]['name'])) ? $value['education'][0]['concentration'][0]['name'] : null ;
$major = "Business Administration";
if (isset($collegemajor)) {
foreach($data as $key=> $value) {
if ($value($collegemajor) == $major) {
echo "User $key is majoring in $major";
}
}
}
So here is the multidimensional array referenced above. In this example, I want to pull the names of all of the user's friends who majored in Business Admin. in college. As you can see from this snippet, there aren't any (I think) but in the long version of the array, there are plenty. The code above produces no output and I'm lost as to how to make it work. Any help would be greatly appreciated.
Array
(
[0] => Array
(
[name] => BLANK
[education] =>
[work] =>
)
[1] => Array
(
[name] => BLANK
[education] => Array
(
[0] => Array
(
[school] => Array
(
[id] => 108087985890571
[name] => St. Andrew's School
)
[year] => Array
(
[id] => 138383069535219
[name] => 2005
)
[type] => High School
)
[1] => Array
(
[school] => Array
(
[id] => 20697868961
[name] => Boston University
)
[concentration] => Array
(
[0] => Array
(
[id] => 108654845832522
[name] => Business Administration
)
)
[type] => College
)
[2] => Array
(
[school] => Array
(
[id] => 108289315859633
[name] => University of Miami
)
[year] => Array
(
[id] => 138879996141011
[name] => 2013
)
[type] => Graduate School
)
)
[work] => Array
(
)
)
[2] => Array
(
[name] => BLANK
[education] => Array
(
[0] => Array
(
[school] => Array
(
[id] => 115444241803885
[name] => Saint Andrews High School
)
[year] => Array
(
[id] => 137616982934053
[name] => 2006
)
[type] => High School
)
[1] => Array
(
[school] => Array
(
[id] => 112033702149888
[name] => Boca Raton High
)
[year] => Array
(
[id] => 137616982934053
[name] => 2006
)
[type] => High School
)
[2] => Array
(
[school] => Array
(
[id] => 108087985890571
[name] => St. Andrew's School
)
[type] => High School
)
[3] => Array
(
[school] => Array
(
[id] => 107573562605861
[name] => Duke University
)
[concentration] => Array
(
[0] => Array
(
[id] => 104045469631213
[name] => Political science
)
)
[type] => College
)
)
[work] =>
)
[4] => Array
(
[uid] => 1234567
[name] => BOB NO ONE
[education] => Array
(
[0] => Array
(
[school] => Array
(
[id] => 106039752760627
[name] => Berwick Academy
)
[year] => Array
(
[id] => 137616982934053
[name] => 2006
)
[type] => High School
)
[1] => Array
(
[school] => Array
(
[id] => 108087985890571
[name] => St. Andrew's School
)
[type] => High School
)
[2] => Array
(
[school] => Array
(
[id] => 105690226130720
[name] => Northeastern University
)
[concentration] => Array
(
[0] => Array
(
[id] => 108654845832522
[name] => Business Administration
)
)
[type] => College
[classes] => Array
(
[0] => Array
(
[id] => 189873264368867
[name] => 2011
)
)
)
)
There's really no need for recursion for something like this, considering the depth of the tree is always fixed and the structure is known. Using some nested loops would do the trick:
$friends = $fqlResult;
$friends_BA = array();
foreach ($friends as $friend) {
if (is_array($friend['education'])) {
foreach ($friend['education'] as $school) {
if (isset($school['concentration'])) {
foreach ($school['concentration'] as $concentration) {
if (strpos(strtolower($concentration['name']), 'business') !== false) {
$friends_BA[] = $friend;
continue 3; // skip to the next friend
}
}
}
}
}
}
var_dump($friends_BA);
You want a function that will find a specific value on a specific field in the array,
function arraySearch($key, $value, $array){
$flag = FALSE;
foreach($array as $result){
if(arraySearch($key, $value, $result)){
$flag = TRUE
}elseif(isset($result[$key] && $result[$key] == $value){
$flag = TRUE;
}
}
return $flag
}
to improve performance rather than setting $flag to true, you could return true as it will stop the execution of the function and prevent it from continuing to search the array.
Call it like so
foreach($fqlResult as $result){
if(arraySearch('concentration', 'Business Administration', $result)){
//You have found a user you are looking for, echo $result['name'] or do what you want with the result.
}
}

Remove duplicates from multi-dimensional array based on higher points

I'm racking my brains trying to think of a solution. I can find plenty of solutions to remove dupes from a 2d array but I need to remove dupes where a value is lower than the other. Here is the array:
Array
(
[basketball] => Array
(
[0] => stdClass Object
(
[id] => 2
[username] => Beans
[points] => 30
)
[1] => stdClass Object
(
[id] => 314
[username] => slights
[points] => 20
)
[2] => stdClass Object
(
[id] => 123
[username] => gibb54
[points] => 5
)
)
[soccer] => Array
(
[0] => stdClass Object
(
[id] => 2
[username] => Beans
[points] => 95
)
[1] => stdClass Object
(
[id] => 49
[username] => sans
[points] => 65
)
[2] => stdClass Object
(
[id] => 122
[username] => peano
[points] => 50
)
[3] => stdClass Object
(
[id] => 174
[username] => fordb
[points] => 30
)
[4] => stdClass Object
(
[id] => 112
[username] => danc
[points] => 30
)
)
)
As you may see, user ID 2, Beans is the first selection for both basketball and soccer. As they have more points for soccer, I need to remove their entry for basketball to make ID 314, slights the 0 value.
I would need to do this continually until no user be the 0 value for any of the primary array values twice.
I've tried various combinations of foreach solutions but I'm not getting anywhere. I thought a while loop would be more suitable but I don't know what condition to test for.
Any ideas please?!
I would loop through your data and create a dictionary where the keys are the user ids, and the values are the appropriate user objects with the sport appended. Then you can reconstruct your example data array structure by looping through this de-duped array using the sport data to determine where to put each user.
To create the de-duped array, use something like:
$deDupedData = array();
foreach ($data as $sport => $users) {
foreach ($users as $user) {
if (isset($deDupedData[$user->id])) {
if ($user->points > $deDupedData[$user->id]->points) {
$deDupedData[$user->id]->sport = $sport;
$deDupedData[$user->id]->points = $user->points;
}
} else {
$modifiedUser = $user;
$modifiedUser->sport = $sport;
$deDupedData[$user->id] = $modifiedUser;
}
}
}
// Now reconstruct your array...

Categories