duanjiagu0655 2015-12-30 15:38
浏览 11
已采纳

随机化具有相同值的元素

Hello stackoverflow community. I need help with arrays. It's my weakness. I've got this kind of array:

Array
(
    [0] => Array
        (
            [id] => 7
            [slot] => 1
            [name] => Apple
            [start_date] => 12/16/2015
            [end_date] => 03/10/2016
            [status] => 1
            [pre_exp_email] => 0
        )

    [1] => Array
        (
            [id] => 8
            [slot] => 1
            [name] => Cherry
            [start_date] => 12/29/2015
            [end_date] => 03/20/2016
            [status] => 1
            [pre_exp_email] => 0
        )

    [2] => Array
        (
            [id] => 5
            [slot] => 3
            [name] => Bananna
            [start_date] => 11/30/2015
            [end_date] => 00/00/0000
            [status] => 1
            [pre_exp_email] => 0
        )

    [3] => Array
        (
            [id] => 1
            [slot] => 4
            [name] => Kiwi
            [start_date] => 11/21/2015
            [end_date] => 12/21/2016
            [status] => 1
            [pre_exp_email] => 0
        )

)

And my job is to randomize elements which has same [slot], but leave order ascending. For example now it is:

1 Apple 1 Cherry 3 Bannana 4 Kiwi

I need to randomize those elements who has same slot number. So Apple and Cherry would swap positions. How can I do this stuff?

  • 写回答

2条回答 默认 最新

  • doumen5491 2015-12-30 15:42
    关注

    Make a new array from the original having slot as keys

    $elements = array(
        0 => Array
            (
                'id' => 7,
                'slot' => 1
            ),
    
        1 => Array
            (
                'id' => 8,
                'slot' => 1
            ),
    
        2 => Array
            (
                'id' => 9,
                'slot' => 1
            ),
        3 => Array
            (
                'id' => 9,
                'slot' => 5
            )
    );
    
    foreach($elements as $element){
        $newArray[$element['slot']][] = $element; //put every element having the same slot
    }
    $elementSlots = array_keys($newArray); // all slots are stored in elementSlots
    $Result = array();
    foreach($elementSlots as $slot) {
        shuffle($newArray[$slot]); //randomize elements having the same slot
        foreach($newArray[$slot] as $element) { //add them to the result array
            $Result[$slot][] = $element;//For output 1
            //$Result[] = $element; //For output 2
        }
    }
    var_dump($Result);
    

    Output 1:

    array (size=2)
      1 => 
        array (size=3)
          0 => 
            array (size=2)
              'id' => int 7
              'slot' => int 1
          1 => 
            array (size=2)
              'id' => int 9
              'slot' => int 1
          2 => 
            array (size=2)
              'id' => int 8
              'slot' => int 1
      5 => 
        array (size=1)
          0 => 
            array (size=2)
              'id' => int 9
              'slot' => int 5
    

    Output 2:

    array (size=4)
      0 => 
        array (size=2)
          'id' => int 7
          'slot' => int 1
      1 => 
        array (size=2)
          'id' => int 9
          'slot' => int 1
      2 => 
        array (size=2)
          'id' => int 8
          'slot' => int 1
      3 => 
        array (size=2)
          'id' => int 9
          'slot' => int 5
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了