PHP旋转并添加到数组 [英] PHP Rotate and adding to an array
问题描述
是否可以在PHO中执行以下操作
Is it possible to do something like the following in PHO
我从数据库返回的数组如下所示
I return an array from my DB that looks like the following
1、2、3现在我要向其添加第四个数字,然后再求助于此
1, 2 ,3 now i want to add a forth number to it and then resort them so
1 becomes 5
2 becomes 1
3 becomes 2
Fourth Number becomes 3
或有可以重新排列我的桌子的地方,它可以旋转。我想做的是旋转并按顺序返回数据库中的一行,但是一旦返回,我想进入我的页面,我想让用户在数据库中输入另一行,然后将已经返回的行放入数据库中
or is there a away to rearrange my table so it rotates. What i am trying to do is rotate and return a row in the database in order but once it has been returned i want to my page i want to get a user to enter another row into my database and then put the row that had been returned to the end so it would have the highest value.
我基本上是通过从所有条目中减去一个并将第一个条目增加到最高编号来更新我的列
I am basically updating my column by subtracting one from all entries and increaseing my first entry to the highest number
a. 1,2,3
b. 4,1,2,3
c. 3,5,1,2,4
d. 2,4,6,1,3,5
嗯,我不认为我说的是正确的我正在寻找的东西,或者有人理解我要说的东西,所以我不会更改它。
Hmmmm I dont think what i am saying is exacty what i am looking for or maybe someone understands what i am trying to say so i wont change it.
或者如果我可以使用从1开始的自动递增键,重新排列其他列中的所有数据将起作用。
Or if i could use a auto incremented key starting at 1 and just rearrange all the data in the other columns would work.
所以
这样做1.计算
行的数量2.返回带有以下内容的数组所有数据库表条目
2.将新条目添加到数据库的最后一行。
3.将1中的条目移至+1行数(创建新行
4.将其他所有条目移至上一行。
So do this 1. Count the number of row 2. Return an array with all database table entries 2. Add a new entry to the database on the last row. 3. Move the entry in 1 to the number of rows +1 (creating a new row 4. Move all other entries up one.
所以这里是
1 -> a
2 -> b
3 -> c
添加另一行后
1 -> b
2 -> c
3 -> d
4 -> a
添加另一行后
1 -> c
2 -> d
3 -> a
4 -> e
5 -> b
添加另一行后
1 -> d
2 -> a
3 -> e
4 -> b
5 -> f
5 -> c
添加另一行后
1 -> a
2 -> e
3 -> b
4 -> f
5 -> c
5 -> g
5 -> d
WHO可以为我提供解决方案?????
WHO can come up with a solution for me?????
推荐答案
您可以使用以下函数并将shifting参数传递给它。函数名称是下面的rotate_array()。使用该函数后,添加下一个值。
You can use the below function and pass the shifting parameter to it. The function name is rotate_array() below. After using the function add the next value.
<?php
$weekdays = array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
function rotate_array($steps, $arr)
{
if($steps >= 0)
{
for($i = 0; $i < $steps; $i++)
{
$elm = array_shift($arr);
array_push($arr, $elm);
}
}
else
{
for($i = 0; $i > $steps; $i--)
{
$elm = array_pop($arr);
array_unshift($arr, $elm);
}
}
return $arr;
}
$arr = rotate_array(1, $weekdays);
//For multidimensional array, add this part
foreach($arr as $key => $childArray) {
rotate_array(1,$childArray);
}
//end of addition
$arr[] = "next value";
echo "<pre>";
print_r($arr);
echo "</pre>";
?>
输出:
Array
(
[0] => Tue
[1] => Wed
[2] => Thu
[3] => Fri
[4] => Sat
[5] => Sun
[6] => Mon
[7] => next value
)
这篇关于PHP旋转并添加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!