duanfen2008 2013-08-14 20:48
浏览 25
已采纳

如何在数组中存储数组的随机部分,然后作为列表打印?

Follow-up question: How to randomly take some examples from arrays, shuffle them and place them into a new array to be formatted as a list

How do you store random parts of an array within an array and then print as a list?

I know that there are answers to both parts of my question already, but the two answers I found aren't compatible with each other. I'm trying to do this with PHP, but i'm willing to use another code language if absolutely necessary.

I made up a simple version of what I've tried to do so far. I would like to store one random question from each array ($mars, $jupiter and $earth) as another array ($all) and then reformat this array as a list to be displayed as html (using < ul > and < li >).

For some reason, the first function wants the =>array(), but the second doesn't.

This makes a formatted html-style list from an array (replace $all with the array you want to format)

function makeList($all) {
        //Base case: an empty array produces no list
        if (empty($all)) return '';
        //Recursive Step: make a list with child lists
        $output = '<ul>';
        foreach ($all as $key => $subArray) {
            $output .= '<li>' . $key . makeList($subArray) . '</li>';
        }
        $output .= '</ul>';
        return $output;
    }

Here I'm just making the arrays. The =>array() is needed for the formatting above. I don't know why.

$mars = array ('How big is Mars?'=>array(), 'How many moons does Mars have?'=>array(), 'How far away is Mars?'=>array(), 'What is the highest point on Mars?'=>array());
$jupiter = array ('How big is Jupiter?'=>array(), 'How many moons does Jupiter have?'=>array(), 'How far away is Jupiter?'=>array(), 'What is the highest point on Jupiter?'=>array());
$earth = array ('How big is Earth?'=>array(), 'How many moons does Earth have?'=>array(), 'How far away is Earth?'=>array(), 'What is the highest point on Earth?'=>array());
//An array of the three arrays
$all = array ($mars, $jupiter, $earth);

This prints the formatted version of $all

echo makeList ($all);

This is randomly taking two questions from the array $marz and printing them

$marz = array ('How big is Mars?', 'How many moons does Mars have?', 'How far away is Mars?', 'What is the highest point on Mars?');
$rand_keys = array_rand($marz, 2);
echo $marz[$rand_keys[0]] . "
";
echo $marz[$rand_keys[1]] . "
";

Here I am storing these results in a new array and printing it as proof in ugly array form

$new = array ($marz[$rand_keys[0]], $marz[$rand_keys[1]]);
print_r ($new);
  • 写回答

1条回答 默认 最新

  • drqyxkzbs21968684 2013-08-14 21:07
    关注

    I don't know if I get what you need, but I found your code really chaotic, so I created my own function from your description, here's whole with repaired arrays and everything

    $mars = array ('How big is Mars?', 'How many moons does Mars have?', 'How far away is Mars?', 'What is the highest point on Mars?');
    $jupiter = array ('How big is Jupiter?', 'How many moons does Jupiter have?', 'How far away is Jupiter?', 'What is the highest point on Jupiter?');
    $earth = array ('How big is Earth?', 'How many moons does Earth have?', 'How far away is Earth?', 'What is the highest point on Earth?');
    
    $all = array($mars, $jupiter, $earth);
    
    function createList($a)
    {
    echo "<ul>";    
    foreach ($a as $array) 
        {
        $questions = count($array);
        $idquestion = rand(0, $questions-1);
        echo "<li>" . $array[$idquestion]  . "</li>";
        }
    echo "</ul>";
    }
    
    createList($all);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面