JKFrox
JKFrox

Reputation: 185

How to return a key of an array from a function where the key is an arg?

I have made a simplified version of what I am trying to explain.

$array ('name' => 1, 'notname' => 1, 'anothername' => 2);

function a($name1, $name2){
  $total = $name1 + $name2;
  $winner = rand(0, $total);

  if ($winner > name2){
    $winner = $name1;
  } else {
    $winner = $name2;
  }

  return $winner;

}

echo a($array['name'], $array['notname']);

Instead of returning (and echoing) the winners number, I want to echo the name of the key for the winner.

I feel like I am making this way to hard.

Upvotes: 0

Views: 59

Answers (4)

Boann
Boann

Reputation: 50041

I'm not really sure what you want, but it sounds to me like you're either trying to find a key based on its position within the array, or a key based on the corresponding value. There are a few functions that might be useful to you.

Start from $example = array('a' => 1, 'b' => 2, 'c' => 3);. The letters are the keys and the numbers are the values.

  • array_keys will return a new array containing the keys of the original array, in order. array_keys($example) will return array(0 => 'a', 1 => 'b', 2 => 'c'). (You can use this to look up a key by position).

  • array_flip will return a new array with the keys exchanged with the values. array_flip($example) will return array(1 => 'a', 2 => 'b', 3 => 'c').

  • array_search will return the key of the first matching value. array_search($example, 2) will return 'b'.


Edit: It sounds like you want to pluck out a key at random?
array_keys($array)[rand(0, count($array) - 1)] ??


Edit 2: Okay, I'm guessing that you mean the values of the array are probabilities of the corresponding name winning. (?)

Here is how I would write that function:

function selectWinner($players) {
    $select = mt_rand() / mt_getrandmax() * array_sum($players);
    $position = 0;
    foreach ($players as $name => $probability) {
        if (($position += $probability) >= $select) return $name;
    }
}

Now assume we have an array:

$players = array('Bob' => 1, 'Bert' => 1, 'Betty' => 2);

Calling selectWinner($players) will return Bob or Bert 25% of the time each, and will return Betty 50% of the time. The probabilities need not be integers.

For testing:

for ($i = 0; $i < 10000; $i++) @$counts[selectWinner($players)]++;
print_r($counts);

This outputs something like:

Array
(
    [Betty] => 4907
    [Bert] => 2560
    [Bob] => 2533
)

Upvotes: 1

William Entriken
William Entriken

Reputation: 39283

The problem is your function a is getting the numbers (array values) and not the array keys. You cannot get an array key from its value.

So you want:

$array ('name' => 1, 'notname' => 1, 'anothername' => 2);

function a($array, $name1, $name2){
  $total = $array[$name1] + $array[$name2];
  $winner = rand(0, $total);
  if ($winner > $array[$name2]){
    return $name1;
  else
    return $name2;
}

echo a($array, 'name', 'notname');

Upvotes: 2

jurasadam
jurasadam

Reputation: 188

First, you have an error in your array declaration, it should be:

$array('name' => 1, 'notname' => 2)

Second, you basically wrote a function that takes a variable as a parameter and then returns it. You are passing only the value to that function, the same as if you'd write echo a('something').

ALso, 'name' is the key of the first value in the array if you write it like this. I think you want to have 1 as the key, 'name' as the value, so you have to write it the other way around.

$array(1 => 'name)

Furthermore, I suggest taking another spin on CS basics :)

Upvotes: 0

Michael.Lumley
Michael.Lumley

Reputation: 2385

Since you said you're working with a more complicated problem in reality, I'm going to assume that

echo 'name';

won't do the trick.

A good way to get keys out of an array is with a foreach loop. For your example above,

foreach($array as $key => $value){ echo $key; }

would return

name notname

I don't know if that helps, but without more info its the best I can do.

Upvotes: 0

Related Questions