0

我有二维数组,我想在第二个数组的随机数上替换第二个二维数组

Array
(
    [1] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 5
            [3] => 500
            [4] => 600
            [5] => 700
        )

    [2] => Array
        (
            [0] => 2
            [1] => 4
            [2] => 6
        )

)

我想得到

 Array (
     [1] => 5 (<- random from first array)     
     [2] => 6 (<- random from second array)    
 )

我试着做:

foreach($variables as $key => $val) {
        $variables = str_replace($val, $val[array_rand($val)], $variables);
}

为什么它不起作用?

4

2 回答 2

2
foreach($variables as $key => $val) {
        $variables[$key] = $val[array_rand($val)];
}
于 2012-09-19T15:24:02.003 回答
0
foreach ($variables as &$var) {
     $var = array_rand($variables[$var]);
}
于 2012-09-19T15:24:16.173 回答