PHP的爆炸阵列 [英] PHP explode array
本文介绍了PHP的爆炸阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图让随机值了一个数组,然后进一步打破他们失望,这里的初始code:
$ =在阵列('FOO_1 | bar_1','FOO_2 | bar_2','foo_3 | bar_3','foo_4 | bar_4','foo_5 | bar_5');
$兰特= array_rand($,第3);$在[$兰特[0]]; // FOO_1 | bar_1
$在[$兰特[1]]; // foo_3 | bar_3
$在[$兰特[2]]; // foo_5 | bar_5
我要的是与上面相同,但每个'富'和'酒吧'通过自己的钥匙,像这样单独访问:
$在[$兰德[0] [0] // FOO_1
$在[$兰特[0] [1] // bar_1$在[$兰特[1]] [0] // foo_3
$在[$兰特[1] [1] // bar_3$在[$兰特[2]] [0] // foo_5
$在[$兰特[2] [1] // bar_5
我试图通过一个foreach循环爆炸$兰特,但我明明做一些错误的n00b:
的foreach($兰特为R $){
$结果=爆炸(|,$ R);
$阵列= $结果;
}
解决方案
试试这个...
$ =在阵列('FOO_1 | bar_1','FOO_2 | bar_2','foo_3 | bar_3','foo_4 | bar_4','foo_5 | bar_5');的foreach(作为&放$,$ R){
$ R =爆炸(|,$ R);
}$兰特= array_rand($,第3);
这是修改 $在
对飞,所以它包含了你要找的嵌套阵列结构。
现在...
$在[$兰德[0] [0] // FOO_1
$在[$兰特[0] [1] // bar_1$在[$兰特[1]] [0] // foo_3
$在[$兰特[1] [1] // bar_3$在[$兰特[2]] [0] // foo_5
$在[$兰特[2] [1] // bar_5
我想这就是你要找的东西。
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;
}
解决方案
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 $in
"on the fly", so it contains the nested array structure you're looking for.
Now...
$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 think that's what you're looking for.
这篇关于PHP的爆炸阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文