简体   繁体   English

如何从 PHP 中的数组中随机选择和删除名称?

[英]How can I randomly select and remove a name from an array in PHP?

I'm a newbie in programming (so don't be too harsh) and our instructors are pushing us to develop using TDD since the beginning.我是编程新手(所以不要太苛刻),我们的讲师从一开始就推动我们使用 TDD 进行开发。

I'm doing a small program that picks a random name.我正在做一个选择随机名称的小程序。 One of my tests attempts to test that the same name is not picked twice, but the test doesn't always pass.我的一项测试试图测试没有两次选择相同的名称,但测试并不总是通过。

This is the function that randomly picks someone.这是随机选择某人的功能。 What I chose the shift method() because it REMOVES the first element of an array.我选择 shift method() 是因为它删除了数组的第一个元素。

public function random($coders) {

        $coders = ['Paul', 'John', 'Brad'];

        shuffle($coders);
        $pickedCoder = array_shift($coders);

        return $pickedCoder;
    }  

This is the test:这是测试:

public function testCoderNotKilledTwice()
    { 
        $coders = ['Paul', 'John', 'Brad'];

        $killer = new Killer();
        $deadCoder1 = $killer->random($coders);
        $deadCoder2 = $killer->random($coders);

        $this->assertNotEquals($deadCoder1, $deadCoder2);
    }

What am I doing wrong?我究竟做错了什么?

"I choosed the shift method() because it REMOVES the first element of an array." “我选择了 shift method() 因为它删除了数组的第一个元素。”

...sure but then $coders = ['Paul', 'John', 'Brad']; ...当然但是$coders = ['Paul', 'John', 'Brad']; inside your "random()" function just re-creates the array anyway each time with the original values.在您的“随机()”函数中,无论如何每次都使用原始值重新创建数组。 The changes you've made using shift() aren't preserved in between calls to "random()".您使用 shift() 所做的更改不会在对“random()”的调用之间保留。 And even if you removed that, every time you call $killer->random($coders);即使你删除了它,每次你调用$killer->random($coders); it passes the original array in as well.它也传入原始数组。

You would need to define $coders as a (private) property at the class level, so its value persists in between calls to the random() function.您需要在类级别将$coders定义为(私有)属性,因此它的值在对 random() 函数的调用之间保持不变。 There's no need to declare $coders within the random() function, nor to pass a copy into the function as an argument.不需要在 random() 函数中声明 $coders ,也不需要将副本作为参数传递给函数。

Something like this:像这样的东西:

class Killer {
  private $coders = ['Paul', 'John', 'Brad'];

  public function random() {
    shuffle($this->coders);
    $pickedCoder = array_shift($this->coders);
    return $pickedCoder;
  }  
}

And then:进而:

public function testCoderNotKilledTwice()
{ 
  $killer = new Killer();
  $deadCoder1 = $killer->random();
  $deadCoder2 = $killer->random();
  $this->assertNotEquals($deadCoder1, $deadCoder2);
}

Bear in mind of course that you might also want to account for the scenario where you no longer have enough items left in the array to return a value.当然,请记住,您可能还想考虑数组中不再有足够的项来返回值的情况。 I don't know how many times you're expecting to be able to run the random() function successfully.我不知道您希望能够成功运行 random() 函数多少次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM