我想生成两个数字序列。我有两个参数。
第一:值可以是1到12之间的任意数字。
第二:值可以是1到12之间的任意数字。
示例
第一名:10, 第二:3,
然后,对于上述参数,我想要两个值如下的数组,
10 11 12 1 2 3 4 5 6 7 8 9
3 4 5 6 7 8 9 10 11 12 1 2
更新:我需要一个将两个数字作为参数并像上述数组那样输出的函数。
有没有简单的方法可以做到这一点?我正在使用php和laravel。
答案 0 :(得分:2)
您可以使用此功能:
function transform($array, int $start) {
while ($array[0] != $start) {
$number = array_shift($array);
$array[] = $number;
}
return $array;
}
$first = transform(range(1, 12), 10);
$second = transform(range(1, 12), 3);
答案 1 :(得分:1)
根据您的输入,我已经创建了函数
void cb2_dsts_rcvd_ex (unsigned int iNumDv, unsigned long *piMicDv, char *pcARName, AIC2_DSTS_STO sto)
{
unsigned int i;
AIC2_DV *pdv;
printf ("\nRemote %s: ", pcARName);
if (sto.bTsNamePres)
printf ("Report (%s/%s) received\n", sto.TsName.pcDomainName, sto.TsName.pcName);
else
printf ("Report (unknown) received\n");
for (i = 0; i < iNumDv; i++)
if (piMicDv[i] != AIC_ID_DV_INVALID)
{
pdv = aic2_get_dv_info (piMicDv[i]);
log_data_values_aic2 (pdv, stdout);
if (pdv) aic2_free_dv_info(pdv);
}
}
您的输入可能是function generateArrayRange($parameterOne='')
{
//mimiminum number in array
$minumumNumber = 1;
//maximum number array number in array
$maximumNumber =12;
//getting the first part of array
$firstArrayDummyFirst = range($parameterOne, $maximumNumber);
//getting the second part of array
$firstArrayDummySecond = range($minumumNumber, $parameterOne-1);
//merging both to the single array
$parmOneArray = array_merge($firstArrayDummyFirst,$firstArrayDummySecond);
//returing the generated array with unique
return ($parmOneArray);
}
那么结果将是
print_r(generateArrayRange(10));
您的输入可能是Array ( [0] => 10 [1] => 11 [2] => 12 [3] => 1 [4] => 2 [5] => 3 [6] => 4 [7] => 5 [8] => 6 [9] => 7 [10] => 8 [11] => 9 )
那么结果将是
print_r(generateArrayRange(3));
您想通过第二个参数
因此,请尝试以下功能:
Array ( [0] => 3 [1] => 4 [2] => 5 [3] => 6 [4] => 7 [5] => 8 [6] => 9 [7] => 10 [8] => 11 [9] => 12 [10] => 1 [11] => 2 )
PHP小提琴
答案 2 :(得分:0)
您可以使用range
:
$first = 5;
$firstArray = range(1,12);
for ($i = 1; $i < $first; $i++) {
$firstArray[11] = array_shift($firstArray);
}
var_dump($firstArray);
对数组进行相同操作。
对于功能:
function shiftedArray($int)
{
$int = $int % 12;
$array = range(1,12);
for ($i = 1; $i < $int; $i++) {
$array[11] = array_shift($array);
}
return $array;
}
只需调用两次:
$firstArray = shiftedArray(10);
$secondArray = shiftedArray(3);
答案 3 :(得分:0)
$first = 3;
$second = 10;
$limit = 12;
$count = 1;
$out1 = $first;
while($out1 <= $limit){
if($out1 == $limit){
echo $out1 . " ";
while($count < $first){
echo $count . " ";
$count++;
}
}else{
echo $out1 . " ";
}
$out1++;
}
echo "<br>";
$count = 1;
$out1 = $second;
while($out1 <= $limit){
if($out1 == $limit){
echo $out1 . " ";
while($count < $second){
echo $count . " ";
$count++;
}
}else{
echo $out1 . " ";
}
$out1++;
}