Php 在循环内洗牌值并与数组连接

Php 在循环内洗牌值并与数组连接,php,arrays,Php,Arrays,我有一个要求,在数组中,我需要洗牌值 下面是代码片段 $vehicle = array('hcv','hcv','hcv','hcv','hcv','hcv','hcv','hcv','hcv','hcv'); $RequiredVehicle = 3; $vehicleList = array(); for($i=0;$i<count($vehicle);$i++) { $vehicleList[] =$vehicle[$i].$RequiredVehicle; } echo

我有一个要求,在数组中,我需要洗牌值

下面是代码片段

$vehicle = array('hcv','hcv','hcv','hcv','hcv','hcv','hcv','hcv','hcv','hcv');
$RequiredVehicle = 3;

$vehicleList = array();


for($i=0;$i<count($vehicle);$i++)
{
  $vehicleList[] =$vehicle[$i].$RequiredVehicle;
}

echo "<pre>";
print_r($vehicleList);
我需要的实际输出是

 $vehicle = array('hcv','hcv','hcv','hcv','hcv','hcv','hcv','hcv','hcv','hcv');
$RequiredVehicle = 3;

$vehicleList = array();

$j = 1 ;
for($i=0;$i<count($vehicle);$i++)
{

  $vehicleList[] =$vehicle[$i].$j++;
  if($j > $RequiredVehicle){
     $j=1;
}
}

echo "<pre>";
print_r($vehicleList);
10辆车辆和3辆所需车辆,因此我需要的输出是总车辆数应在3辆所需车辆之间移动

如果其$vehicle=10和$RequiredVehicle=3,则数组值应为 1,2,3,1,2,3,1,2,3,1

如果其$vehicle=10和$RequiredVehicle=2,则数组值应为
1,2,1,2,1,2,1,1,2,1,2希望它能提供您的实际输出:)


这是因为您只是添加了
$RequiredVehicle

您可以使用模
%
$i
并添加1。比如
($i%$RequiredVehicle)+1)

Array
(
    [0] => hcv1
    [1] => hcv2
    [2] => hcv3
    [3] => hcv1
    [4] => hcv2
    [5] => hcv3
    [6] => hcv1
    [7] => hcv2
    [8] => hcv3
    [9] => hcv1
)
 $vehicle = array('hcv','hcv','hcv','hcv','hcv','hcv','hcv','hcv','hcv','hcv');
$RequiredVehicle = 3;

$vehicleList = array();

$j = 1 ;
for($i=0;$i<count($vehicle);$i++)
{

  $vehicleList[] =$vehicle[$i].$j++;
  if($j > $RequiredVehicle){
     $j=1;
}
}

echo "<pre>";
print_r($vehicleList);
Array
(
    [0] => hcv1
    [1] => hcv2
    [2] => hcv3
    [3] => hcv1
    [4] => hcv2
    [5] => hcv3
    [6] => hcv1
    [7] => hcv2
    [8] => hcv3
    [9] => hcv1
)
$vehicle = array('hcv','hcv','hcv','hcv','hcv','hcv','hcv','hcv','hcv','hcv');
$RequiredVehicle = 3;

$vehicleList = array();
for($i=0;$i<count($vehicle);$i++)
{
   $vehicleList[] =$vehicle[$i] . ( ( $i % $RequiredVehicle ) + 1 );
}

echo "<pre>";
print_r($vehicleList);
Array
(
    [0] => hcv1
    [1] => hcv2
    [2] => hcv3
    [3] => hcv1
    [4] => hcv2
    [5] => hcv3
    [6] => hcv1
    [7] => hcv2
    [8] => hcv3
    [9] => hcv1
)