从平面数组中获取 3 个随机元素,并按分隔符分割每个元素

发布于 2024-09-18 15:16:50 字数 661 浏览 6 评论 0原文

我试图从数组中获取随机值,然后进一步分解它们,这是初始代码:

$in = array('foo_1|bar_1', 'foo_2|bar_2','foo_3|bar_3','foo_4|bar_4','foo_5|bar_5' );
$rand = array_rand($in, 3);

$in[$rand[0]]; //foo_1|bar_1
$in[$rand[1]]; //foo_3|bar_3
$in[$rand[2]]; //foo_5|bar_5

我想要的与上面相同,但每个“foo”和“bar”都可以通过自己的密钥单独访问,例如这个:

$in[$rand[0]][0] //foo_1
$in[$rand[0]][1] //bar_1

$in[$rand[1]][0] //foo_3
$in[$rand[1]][1] //bar_3

$in[$rand[2]][0] //foo_5
$in[$rand[2]][1] //bar_5

我尝试通过 foreach 循环爆炸 $rand 但我显然犯了一些 n00b 错误:

foreach($rand as $r){
    $result = explode("|", $r);  
    $array = $result;
}

I'm trying to get random values out of an array and then break them down further, here's the initial code:

$in = array('foo_1|bar_1', 'foo_2|bar_2','foo_3|bar_3','foo_4|bar_4','foo_5|bar_5' );
$rand = array_rand($in, 3);

$in[$rand[0]]; //foo_1|bar_1
$in[$rand[1]]; //foo_3|bar_3
$in[$rand[2]]; //foo_5|bar_5

What I want is same as above but with each 'foo' and 'bar' individually accessible via their own key, something like this:

$in[$rand[0]][0] //foo_1
$in[$rand[0]][1] //bar_1

$in[$rand[1]][0] //foo_3
$in[$rand[1]][1] //bar_3

$in[$rand[2]][0] //foo_5
$in[$rand[2]][1] //bar_5

I've tried exploding $rand via a foreach loop but I'm obviously making some n00b error:

foreach($rand as $r){
    $result = explode("|", $r);  
    $array = $result;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦忆晨望 2024-09-25 15:16:50

你很接近:

$array = array();
foreach ($in as $r)
    $array[] = explode("|", $r);

You were close:

$array = array();
foreach ($in as $r)
    $array[] = explode("|", $r);
狼亦尘 2024-09-25 15:16:50

试试这个:

$in = array('foo_1|bar_1', 'foo_2|bar_2','foo_3|bar_3','foo_4|bar_4','foo_5|bar_5' );

foreach ($in as &$r) {
  $r = explode("|", $r);  
}

$rand = array_rand($in, 3);

通过引用修改 $in 数组的所有值,因此在循环之后,所有字符串都被拆分为两个子数组元素。

现在,使用 $rand 数组的索引结果访问变异的 $in 数组中的三行。

$in[$rand[0]][0] //foo_1
$in[$rand[0]][1] //bar_1

$in[$rand[1]][0] //foo_3
$in[$rand[1]][1] //bar_3

$in[$rand[2]][0] //foo_5
$in[$rand[2]][1] //bar_5

Try this:

$in = array('foo_1|bar_1', 'foo_2|bar_2','foo_3|bar_3','foo_4|bar_4','foo_5|bar_5' );

foreach ($in as &$r) {
  $r = explode("|", $r);  
}

$rand = array_rand($in, 3);

That modifies all of the values of the $in array by reference, so after the loop, all strings have been split into two subarray elements.

Now, use the indexed results of the$rand array to access three of the rows from the mutated $in array.

$in[$rand[0]][0] //foo_1
$in[$rand[0]][1] //bar_1

$in[$rand[1]][0] //foo_3
$in[$rand[1]][1] //bar_3

$in[$rand[2]][0] //foo_5
$in[$rand[2]][1] //bar_5
紅太極 2024-09-25 15:16:50
foreach($rand as $r){
  $result = explode("|", $r);  
  array_push($array, $result);
}
foreach($rand as $r){
  $result = explode("|", $r);  
  array_push($array, $result);
}
花开浅夏 2024-09-25 15:16:50
  1. 不直观的是,当调用多个随机值时,array_rand() 返回一个键数组。
  2. 当您实际上只想分解 3 个字符串时,分解整个字符串数组会浪费周期。

代码:(演示)

$in = [
    'foo_1|bar_1',
    'foo_2|bar_2',
    'foo_3|bar_3',
    'foo_4|bar_4',
    'foo_5|bar_5'
];

var_export(
    array_map(
        fn($i) => explode('|', $in[$i]),
        array_rand($in, 3)
    )
);

潜在输出:

array (
  0 => 
  array (
    0 => 'foo_2',
    1 => 'bar_2',
  ),
  1 => 
  array (
    0 => 'foo_3',
    1 => 'bar_3',
  ),
  2 => 
  array (
    0 => 'foo_5',
    1 => 'bar_5',
  ),
)
  1. Unintuitively, array_rand() returns an array of keys when more than one random value is called for.
  2. It is a waste of cycles to explode the full array of strings when you only actually want 3 strings to be exploded.

Code: (Demo)

$in = [
    'foo_1|bar_1',
    'foo_2|bar_2',
    'foo_3|bar_3',
    'foo_4|bar_4',
    'foo_5|bar_5'
];

var_export(
    array_map(
        fn($i) => explode('|', $in[$i]),
        array_rand($in, 3)
    )
);

Potential output:

array (
  0 => 
  array (
    0 => 'foo_2',
    1 => 'bar_2',
  ),
  1 => 
  array (
    0 => 'foo_3',
    1 => 'bar_3',
  ),
  2 => 
  array (
    0 => 'foo_5',
    1 => 'bar_5',
  ),
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文