Php 将数组元素移动到第一个位置,但保持顺序
我不知道如何用英语恰当地表达这个问题,因为这个问题看起来很像一个重复的问题,但我的意思并不相同。我想这也是我找不到任何类似问题的原因,所以提前预约,因为这可能是重复的 我想创建一个函数,该函数执行以下操作: 函数重新排序($arr,$number) 等等Php 将数组元素移动到第一个位置,但保持顺序,php,arrays,Php,Arrays,我不知道如何用英语恰当地表达这个问题,因为这个问题看起来很像一个重复的问题,但我的意思并不相同。我想这也是我找不到任何类似问题的原因,所以提前预约,因为这可能是重复的 我想创建一个函数,该函数执行以下操作: 函数重新排序($arr,$number) 等等 谢谢 根据您给出的示例,我假设您希望在提供值之前分割数组,然后将其切换。应该这样做的函数如下所示: function reorder($arr, $value) { $index = array_search($value, $arr)
谢谢 根据您给出的示例,我假设您希望在提供值之前分割数组,然后将其切换。应该这样做的函数如下所示:
function reorder($arr, $value) {
$index = array_search($value, $arr);
// Value does not exist in array
if (false === $index) {
return false;
}
$pre = array_splice($arr, $index);
return array_merge($pre, $arr);
}
/*循环旋转-将数组向右旋转给定的步数。
*$A=由N个整数组成的数组
*$K=K次旋转
*返回具有旋转值的数组。
*/
函数旋转($A,$K){
if(is_数组($A)&&count($A)>0&&count($A)>=K){
对于($i=0;$i请查看此项
$data = array('one','two','three','four','five');
function reorder($dataSet, $offsetValue)
{
// find the index of the offsetValue
$araayIndexPosition = array_search($offsetValue, $dataSet);
$totalElements = count($dataSet);
$newDataList = array();
for ($i=$araayIndexPosition; $i <= $totalElements-1 ; $i++)
{
$newDataList[]=$dataSet[$i];
}
for ($i=0; $i <= $araayIndexPosition-1 ; $i++)
{
$newDataList[]=$dataSet[$i];
}
return $newDataList;
}
echo '<pre>';
print_r(reorder($data, 'four'));
echo '</pre>';
$data=array('1','2','3','4','5');
函数重新排序($dataSet,$offsetValue)
{
//查找偏移值的索引
$araayIndexPosition=array\u search($offsetValue,$dataSet);
$totalElements=计数($dataSet);
$newDataList=array();
对于($i=$arayindexposition;$i这对您来说可能有点过头了:
Hey@Roddeh!我有一个简单的解决方案,看看答案,我想这不是一个重复,请随意修改我的问题,使其适合解决方案。谢谢,这也行。@mi6crazyheart似乎短了一点,只包含一个函数。据我所知,@bfranco的解决方案是最好的数组函数没有循环,只有四行函数!!!如果我必须选择答案,我会选择他!!如果任何答案对你有效,你也应该投票给他们,因为他们已经花了时间!谢谢!!是的,绝对正确!太棒了。有什么建议我应该如何改变我的问题,以符合你给出的解决方案吗?你显然理解ood it:d这回答了这个问题,但是数组操作非常慢。请看我的答案,我使用PHP迭代器创建了一个“内部指针”。这比你的答案更复杂。是的,Dymen1,你的应该更快。看起来快了大约4倍。我把它们都放在了一个小基准测试中。我做了1英里的迭代,你的解决方案用了0.18819093704224秒就完成了,而我的解决方案用了0.67791223526001秒。问题是,这值得额外的开发时间吗?是的,如果这需要在短时间内运行数千次/数百万次,否则我会倾向于称之为过早优化。但考虑到这些情况并没有什么坏处。这看起来不像php,甚至不像我的XD。我不打算使用它,因为这对我来说确实是过度的,我不理解:P
/* Cyclic Rotation - Rotate an array to the right by given number of steps.
* $A = Array consisting N integers
* $K = K times rotation
* Returns Array with rotated values.
*/
function rotate($A,$K){
if(is_array($A) && count($A)>0 && count($A) >= $K){
for($i=0;$i<$K;$i++){
/* $elm = array_pop($A);
array_unshift($A, $elm); */
$elm = array_shift($A);
array_push($A,$elm);
}
}
return $A;
}
/* Fech Key from of the element from array
* $A = Array
* $ele = string/value of array
* Returns Array of rotated values.
*/
function fetchKey($A,$ele){
$val = array();
if(in_array($ele,$A)){
$val = array_keys($A,$ele);
}
return rotate($A,$val[0]);
}
$arr = array('one','two','three','four','five');
$val = fetchKey($arr,'two');
var_export($val);
$data = array('one','two','three','four','five');
function reorder($dataSet, $offsetValue)
{
// find the index of the offsetValue
$araayIndexPosition = array_search($offsetValue, $dataSet);
$totalElements = count($dataSet);
$newDataList = array();
for ($i=$araayIndexPosition; $i <= $totalElements-1 ; $i++)
{
$newDataList[]=$dataSet[$i];
}
for ($i=0; $i <= $araayIndexPosition-1 ; $i++)
{
$newDataList[]=$dataSet[$i];
}
return $newDataList;
}
echo '<pre>';
print_r(reorder($data, 'four'));
echo '</pre>';
class myIterator implements Iterator {
private $position = 0;
private $array = array(
'one',
'two',
'three',
'four',
'five'
);
public function __construct() {
$this->position = 0;
}
public function rewind() {
$this->position = 0;
}
public function current() {
return $this->array[$this->position];
}
public function key() {
return $this->position;
}
public function next() {
++$this->position;
if ($this->position >= count($this->array)) $this->rewind();
}
public function valid() {
return isset($this->array[$this->position]);
}
public function reorder($desiredFirstElement) {
foreach ($this->array as $ele) {
if ($this->current() === $desiredFirstElement) return;
$this->next();
}
}
public function toArray() {
$returnArray = [];
foreach ($this->array as $ele) {
$returnArray[] = $this->current();
$this->next();
}
return $returnArray;
}
}
$it = new myIterator;
$it->reorder('two'); // => 'two','three','four','five','one'
var_dump($it->toArray());
$it->reorder('four'); // => 'four','five','one','two','three'
var_dump($it->toArray());
$it->reorder('five'); // => 'five','one','two','three','four'
var_dump($it->toArray());