what is the best way to create an array (with unordered indexes) using next index value into the current index. i tried to use next() but didn't help. what i mean is:
使用当前索引中的下一个索引值创建数组(使用无序索引)的最佳方法是什么。我试图使用next()但没有帮助。我的意思是:
Array
(
[0] => a
[8] => b
[2] => c
[7] => d
[9] => e
[11] => f
)
so, i want this data as an array like bellow
所以,我希望这些数据像下面的数组一样
Array
(
[0] => Array
(
[current] => a
[next] => b
)
[8] => Array
(
[current] => b
[next] => c
)
[2] => Array
(
[current] => c
[next] => d
)
[7] => Array
(
[current] => d
[next] => e
)
[9] => Array
(
[current] => e
[next] => f
)
[11] => Array
(
[current] => f
[next] =>
)
)
what could be the fastest way to do that.
什么是最快的方式来做到这一点。
2 个解决方案
#1
0
UPDATE
UPDATE
For in unordered array
对于无序数组
$newarray = array();
$length = count($oldarray);
for($i = 0; $i < $length ; ++$i)
{
$newarray[] = array('current'=>current($oldarray),'next'=>next($oldarray));
}
#2
2
Note: this solution actually works, as proven. It was downvoted for some obscure reason.
注意:这个解决方案确实有效,经过验证。由于一些不明原因,它被推翻了。
The simplest solution I can think is the one you've suggested on your own question, with a slight improvement (Working Demo)
我能想到的最简单的解决方案是你在自己的问题上提出的解决方案,略有改进(Working Demo)
<?php
// simulate the array
$arr = [0=>'a',8=>'b',2=>'c',7=>'d',9=>'e',11=>'f'];
// iterate it without messing with its internal pointer
for($i=0;$i<count($arr);$i++)
$arr[key($arr)] = ["current" => current($arr), "next" => next($arr)];
// test it
echo '<pre>';
print_r($arr);
OUTPUT
OUTPUT
Array
(
[0] => Array
(
[current] => a
[next] => b
)
[8] => Array
(
[current] => b
[next] => c
)
[2] => Array
(
[current] => c
[next] => d
)
[7] => Array
(
[current] => d
[next] => e
)
[9] => Array
(
[current] => e
[next] => f
)
[11] => Array
(
[current] => f
[next] =>
)
)
Your goal with this question is the exact purpose of current()
, next()
and key()
functions, so you just have to use them.
这个问题的目标是current(),next()和key()函数的确切目的,所以你只需要使用它们。
#1
0
UPDATE
UPDATE
For in unordered array
对于无序数组
$newarray = array();
$length = count($oldarray);
for($i = 0; $i < $length ; ++$i)
{
$newarray[] = array('current'=>current($oldarray),'next'=>next($oldarray));
}
#2
2
Note: this solution actually works, as proven. It was downvoted for some obscure reason.
注意:这个解决方案确实有效,经过验证。由于一些不明原因,它被推翻了。
The simplest solution I can think is the one you've suggested on your own question, with a slight improvement (Working Demo)
我能想到的最简单的解决方案是你在自己的问题上提出的解决方案,略有改进(Working Demo)
<?php
// simulate the array
$arr = [0=>'a',8=>'b',2=>'c',7=>'d',9=>'e',11=>'f'];
// iterate it without messing with its internal pointer
for($i=0;$i<count($arr);$i++)
$arr[key($arr)] = ["current" => current($arr), "next" => next($arr)];
// test it
echo '<pre>';
print_r($arr);
OUTPUT
OUTPUT
Array
(
[0] => Array
(
[current] => a
[next] => b
)
[8] => Array
(
[current] => b
[next] => c
)
[2] => Array
(
[current] => c
[next] => d
)
[7] => Array
(
[current] => d
[next] => e
)
[9] => Array
(
[current] => e
[next] => f
)
[11] => Array
(
[current] => f
[next] =>
)
)
Your goal with this question is the exact purpose of current()
, next()
and key()
functions, so you just have to use them.
这个问题的目标是current(),next()和key()函数的确切目的,所以你只需要使用它们。