Php 如何从随机选择的值中获取非随机值?

Php 如何从随机选择的值中获取非随机值?,php,arrays,random,variable-assignment,Php,Arrays,Random,Variable Assignment,我正在尝试为随机选择的值选择一个非随机值 第一列表示销售编号(1-30) 第二列显示必须随机选择的项目 第三列显示所述项目的相应价格 如果项目是随机选择的,我如何实现这一点 我曾想过使用switch语句,但这可能会变得混乱 $items = array( ‘clock’ , ‘kettle’ , ‘mug’ , ‘toaster’ , ‘CD’); $prices = array( '£30', '£19', '£5' , '£14' , '£7'); for($i = 0; $i <

我正在尝试为随机选择的值选择一个非随机值

  • 第一列表示销售编号(1-30)
  • 第二列显示必须随机选择的项目
  • 第三列显示所述项目的相应价格
如果项目是随机选择的,我如何实现这一点

我曾想过使用switch语句,但这可能会变得混乱

$items = array( ‘clock’ , ‘kettle’ , ‘mug’ , ‘toaster’ , ‘CD’);
$prices = array( '£30', '£19', '£5' , '£14' , '£7');

for($i = 0; $i < 30; $i++) {
    $sales[$i][0] = $i + 1; //Starts at 1 and increments
    $sales[$i][1] =  $items[rand(0,4)]; //Item chosen at random
    $sales[$i][2] = //Should be the corresponding price for the above item.
}
$items=数组('clock','kettle','mug','toaster','CD');
$prices=数组('30英镑'、'19英镑'、'5英镑'、'14英镑'、'7英镑');
对于($i=0;$i<30;$i++){
$sales[$i][0]=$i+1;//从1开始,递增
$sales[$i][1]=$items[rand(0,4)];//随机选择的项目
$sales[$i][2]=//应为上述项目的相应价格。
}
$items=数组('clock'、'kettle'、'mug'、'toaster'、'CD');
$prices=数组('30英镑'、'19英镑'、'5英镑'、'14英镑'、'7英镑');
对于($i=0;$i<30;$i++){
$sales[$i][0]=$i+1;//从1开始,递增
$sales[$i][1]=$items[rand(0,4)];//随机选择的项目
$sales[$i][2]=//应为上述项目的相应价格
}
观察和学习:

$items = array( ‘clock’ , ‘kettle’ , ‘mug’ , ‘toaster’ , ‘CD’);
$prices = array( '£30', '£19', '£5' , '£14' , '£7');

for($i = 0; $i < 30; $i++) {
    $random = rand(0,4);

    $sales[$i][0] = $i + 1; //Starts at 1 and increments
    $sales[$i][1] = $items[$random]; //Item chosen at random
    $sales[$i][2] = $prices[$random];
}
$items=数组('clock','kettle','mug','toaster','CD');
$prices=数组('30英镑'、'19英镑'、'5英镑'、'14英镑'、'7英镑');
对于($i=0;$i<30;$i++){
$random=兰特(0,4);
$sales[$i][0]=$i+1;//从1开始,递增
$sales[$i][1]=$items[$random];//随机选择的项目
$sales[$i][2]=$prices[$random];
}

$random=rand(0,4)$销售额[$o][1]=$items[$random]$销售额[$i][2]=$items[$random]?!显示
$sales
阵列或存储定价信息的任何位置。没有这些,我们就不知道如何帮助你。目前尚不清楚您如何将
$items
中的项目与任何销售定价关联。。。或
array\u rand()
@MikeBrant道歉,已编辑。将“$random=rand(0,4);”在循环内部,然后使用$items[$random]$价格[$随机];工作?为什么不将物品和价格存储在一个单一的多维数组中<代码>$data=数组(数组('item'=>'时钟','price'=>30),数组('item'=>'水壶','price'=>19),…)
$items = array( ‘clock’ , ‘kettle’ , ‘mug’ , ‘toaster’ , ‘CD’);
$prices = array( '£30', '£19', '£5' , '£14' , '£7');

for($i = 0; $i < 30; $i++) {
    $random = rand(0,4);

    $sales[$i][0] = $i + 1; //Starts at 1 and increments
    $sales[$i][1] = $items[$random]; //Item chosen at random
    $sales[$i][2] = $prices[$random];
}