Loop through an array

Hello.

I’ve got a problem , which I couldn’t solve , that’s why I am writing.

So …
I have a 2 variables in which I have a data i.e 1-st variable → amount of players from a certain world. , and 2-nd variable → data of 300 best players from certain world.

I need a script which would check who from these 300 best players is online and for each of them mark with a star (*) .
I tried to do it with a function array_intersect but unfortunately I’ve got nothing.

Something in this kind \/

$array1 = array($table[$i]['name']);
$array2 = array($myresult[$i]['name']);
$result = array_intersect($array1, $array2);

Any ideas?

We don’t know the contents or structure of your $table and $myresult variables, and it’s kinda needed to help you. Consider posting samples of them using var_export()

var_export($table)

An array with sizeof - 366 records.

array (
  0 => 
  array (
    'name' => 'Aagiz Shirobashi',
  ),
  1 => 
  array (
    'name' => 'Admiral Olympa',
  ),
  2 => 
  array (
    'name' => 'Aern Sath',
  ),
  3 => 
  array (
    'name' => 'Aklikas',
  ),
  4 => 
  array (
    'name' => 'Aldozo',
  ),

// ************************************//


var_export($myresult)

An array with sizeof - 300 records.

array (
  0 => 
  array (
    'name' => 'Szuszkin',
  ),
  1 => 
  array (
    'name' => 'Lord Executer',
  ),
  2 => 
  array (
    'name' => 'Amorozo the Knight',
  ),
  3 => 
  array (
    'name' => 'Byna',
  ),
  4 => 






Ok. If you wanted to use a built in php function, you would need to use array_uintersect() and write your own comparison function.


function intersection_comparator($a, $b) {
    return strcmp($a['name'], $b['name']);
}

You could also just do the intersection manually


$map = array();
foreach ($myresult as $player) {
    $map[ $player['name'] ] = 1;
}

$intersection = array();
foreach ($table as $playerr) {
    if (isset($map[ $player['name'] ])) {
        $intersection[] = $player;
    }
}

Thanks for sharing a part of your wisdom.

It works.

@refresh

How Could I do a separate array for a desired profession of player?

first reset first table , then in another foreach select only this with desired vocation(knight,sorcerer,druid,paladin) e.g set a value for 1.

profession is getting by

$intersection[$i]['vocation']

but how it should present in php code?

If I understand correctly


$playersGroupedByVocation = array();
foreach ($players as $player) {
    $playersGroupedByVocation[ $player['vocation'] ][] = $player;
}

print_r($playersGroupedByVocation['druid']);

//or if you just need a single type
$warlocks = array();
foreach ($players as $player) {
    if ($player['vocation'] === 'warlock') {
        $warlocks[] = $player;
    }
}

I have this feeling this data might be coming from a database. If so, it could be much more efficient to use database features do the filtering(unless, you really need all the data).

It’s getting by cURL.

btw. that’s what I needed.

Is there a way to consider promoted players same as normal?

e.g. promoted player have a special rank - i.e Elite(Knight) , normal have just Knight , can I get all of them to one array?


$warlocks = array(); 
foreach ($players as $player) { 
    if ($player['vocation'] === 'warlock' || $player['vocation'] === 'Elite(warlock)') { 
        $warlocks[] = $player; 
    }
}

If you have a lot of these variations, the code could get large and repetitive. There’s other ways you could do it if that’s the case.