如果它是唯一的,如何只将数据包含在foreach中的数组中?

时间:2018-02-16 04:28:28

标签: php arrays sorting

我有一个返回随机水果数据的函数。我想每次都返回5种不同的水果,不应该有不止一次的相同值。

以下是我遇到问题的示例https://eval.in/957206(它生成香蕉两次),下面是编写的代码。

function generateFruit(){
  $data = array(
      0 => array( 'id' => 1, 'fruit' => array('title' => 'Banana')),
      1 => array( 'id' => 2, 'fruit' => array('title' => 'Strawberry')),
      2 => array( 'id' => 3, 'fruit' => array('title' => 'Orange')),
      3 => array( 'id' => 4, 'fruit' => array('title' => 'Kiwi')),
      4 => array( 'id' => 5, 'fruit' => array('title' => 'Apple')),
      5 => array( 'id' => 6, 'fruit' => array('title' => 'Apricots')),
      6 => array( 'id' => 7, 'fruit' => array('title' => 'Avacado')),
      7 => array( 'id' => 8, 'fruit' => array('title' => 'Blueberries')),
      8 => array( 'id' => 9, 'fruit' => array( 'title' => 'Cranberries')),
      9 => array( 'id' => 10, 'fruit' => array( 'title' => 'Figs')),
      10 => array( 'id' => 11, 'fruit' => array( 'title' => 'Grapes')),
  );

  return $data[array_rand($data)];
}

function generateAllFruits(){
    foreach(range(1, 5) as $key => $useless){
        $data[$key] = array(
            'fruits' => generateFruit()
        );
    }

    print_r($data);
}

generateAllFruits();

我无法使用array_unique,因为它会删除重复项,而不是再次运行generateFruit代码并返回不在$data数组中的新值。

我可以使用in_array,但它是一个多维数组,如果没有column_array作为第二个参数,它将无法工作,至少这是我能想到让它工作的方式,除了..它没有。

3 个答案:

答案 0 :(得分:3)

shuffle(随机化)数组,然后选择前5:

shuffle($data);
$result = array_slice($data, 0, 5);

答案 1 :(得分:0)

我采取了略微不同的方法。我返回了数组,以便我可以修改和删除数组中的元素,如下所示:

function getFreshFruits() {
  return array(
    0 => array( 'id' => 1, 'fruit' => array('title' => 'Banana')),
    1 => array( 'id' => 2, 'fruit' => array('title' => 'Strawberry')),
    2 => array( 'id' => 3, 'fruit' => array('title' => 'Orange')),
    3 => array( 'id' => 4, 'fruit' => array('title' => 'Kiwi')),
    4 => array( 'id' => 5, 'fruit' => array('title' => 'Apple')),
    5 => array( 'id' => 6, 'fruit' => array('title' => 'Apricots')),
    6 => array( 'id' => 7, 'fruit' => array('title' => 'Avacado')),
    7 => array( 'id' => 8, 'fruit' => array('title' => 'Blueberries')),
    8 => array( 'id' => 9, 'fruit' => array( 'title' => 'Cranberries')),
    9 => array( 'id' => 10, 'fruit' => array( 'title' => 'Figs')),
    10 => array( 'id' => 11, 'fruit' => array( 'title' => 'Grapes')),
  );
}

在那之后,这很容易。

$freshFruits = getFreshFruits();
$pickedFruits = array();

$i = 0;
while ($i < 5) {
  $rando = rand(0,count($freshFruits)-1);
  array_push($pickedFruits,$freshFruits[$rando]);
  array_splice($freshFruits,$rando,1);
  $i++;
}

echo "<pre>";
print_r($pickedFruits);
echo "</pre>";

Array
(
    [0] => Array
        (
            [id] => 3
            [fruit] => Array
                (
                    [title] => Orange
                )

        )

    [1] => Array
        (
            [id] => 6
            [fruit] => Array
                (
                    [title] => Apricots
                )

        )

    [2] => Array
        (
            [id] => 5
            [fruit] => Array
                (
                    [title] => Apple
                )

        )

    [3] => Array
        (
            [id] => 9
            [fruit] => Array
                (
                    [title] => Cranberries
                )

        )

    [4] => Array
        (
            [id] => 7
            [fruit] => Array
                (
                    [title] => Avacado
                )

        )

)

答案 2 :(得分:0)

如果你想使用array_rand函数,你应该使用第二个参数(返回值的数量),如下所示。

$alldata = array(
  0 => array( 'id' => 1, 'fruit' => array('title' => 'Banana')),
  1 => array( 'id' => 2, 'fruit' => array('title' => 'Strawberry')),
  2 => array( 'id' => 3, 'fruit' => array('title' => 'Orange')),
  3 => array( 'id' => 4, 'fruit' => array('title' => 'Kiwi')),
  4 => array( 'id' => 5, 'fruit' => array('title' => 'Apple')),
  5 => array( 'id' => 6, 'fruit' => array('title' => 'Apricots')),
  6 => array( 'id' => 7, 'fruit' => array('title' => 'Avacado')),
  7 => array( 'id' => 8, 'fruit' => array('title' => 'Blueberries')),
  8 => array( 'id' => 9, 'fruit' => array( 'title' => 'Cranberries')),
  9 => array( 'id' => 10, 'fruit' => array( 'title' => 'Figs')),
  10 => array( 'id' => 11, 'fruit' => array( 'title' => 'Grapes')),
);

foreach (array_rand($alldata,5) as $k => $v) $data[$k][fruits]=$alldata[$v];
print_r($data);

见这里:https://eval.in/957212。您不需要两个功能。