如何从数组中一次检索 3 个数据?

发布于 2024-09-26 12:07:22 字数 497 浏览 1 评论 0原文

PHP 中是否有更好的方法以三个一组的方式访问该数组?

假设我的数组是一个逗号分隔的字符串,例如:

("Thora","Birch","Herself","Franklin Delano","Roosevelt","Himself (archive footage)","Martin Luther","King","Himself (archive footage) (uncredited)")

虽然数组最终可能会更大,但每个数组都会有所不同,但仍处于三个逻辑组中(名字、姓氏、角色)。

我想要完成的是以三组为一组循环遍历数组,以便第一条记录具有 firstname[0]=>"Thora"、lastname[0]=>"Birch" 和 role [0]=>“Herself”。

我只习惯于foreach循环,但没有看到像“for every 3”这样的额外参数,所以也许有人可以解释一下?

Is there a better way in PHP to access this array in groups of three?

Let's say my array is a comma separated string like:

("Thora","Birch","Herself","Franklin Delano","Roosevelt","Himself (archive footage)","Martin Luther","King","Himself (archive footage) (uncredited)")

Although the array can end up being much larger, each one will be different but still in logical groups of three (firstname, lastname, role).

What I'm trying to accomplish is looping through the array in groups of three, so that the first record would have firstname[0]=>"Thora", lastname[0]=>"Birch" and role[0]=>"Herself".

I'm only used to foreach loops but don't see an extra parameter like "for each 3" so maybe someone can shed some light?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜唯美灬不弃 2024-10-03 12:07:22

PHP 有一个名为 array_chunk 的函数专门用于此任务。

试试这个:

$sets = array_chunk($people, 3);
foreach ($sets as $set) {
  //you could use the following line to put each field into a nice variable
  list ($firstname, $lastName, $role) = $set;

  //if all you want to do is collect the values, do this 
  $firstNames[] = $set[0];
  $lastNames[] = $set[1];
  $role[] = $set[2];
}

请注意,最后一个块的大小可能不是 3,因此如果您不能保证数组的长度始终是 3 的倍数,则应该检查键 1 和 2 是否使用 isset 设置。

PHP has a function called array_chunk designed for this task.

Try this:

$sets = array_chunk($people, 3);
foreach ($sets as $set) {
  //you could use the following line to put each field into a nice variable
  list ($firstname, $lastName, $role) = $set;

  //if all you want to do is collect the values, do this 
  $firstNames[] = $set[0];
  $lastNames[] = $set[1];
  $role[] = $set[2];
}

Note that the last chunk might not be of size 3, so you should check that keys 1 and 2 are set with isset, if you can't guarantee that your array is always a multiple of 3 in length.

陈甜 2024-10-03 12:07:22

您可以使用 for 循环来实现此目的,假设您的“people”数组的长度为 3n

$people = array("Thora","Birch","Herself","Franklin Delano","Roosevelt","Himself (archive footage)","Martin Luther","King","Himself (archive footage) (uncredited)");
$firstName = array();
$lastName = array();
$role = array();
for($i=0; $i<count($people); $i += 3){
   $firstName[] = $people[$i];
   $lastName[] = $people[$i+1];
   $role[] = $people[$i+2];
}

You can use a for-loop for this, assuming your 'people' array has a length of 3n.

$people = array("Thora","Birch","Herself","Franklin Delano","Roosevelt","Himself (archive footage)","Martin Luther","King","Himself (archive footage) (uncredited)");
$firstName = array();
$lastName = array();
$role = array();
for($i=0; $i<count($people); $i += 3){
   $firstName[] = $people[$i];
   $lastName[] = $people[$i+1];
   $role[] = $people[$i+2];
}
邮友 2024-10-03 12:07:22

怎么样
伪代码

for Index = 0 index = length index +=3
{
   Firstname= array[index]
   middlename = array[index+1]
   lastname = array[index+2]
   Do Something with them...
}

How about
Pseudo code

for Index = 0 index = length index +=3
{
   Firstname= array[index]
   middlename = array[index+1]
   lastname = array[index+2]
   Do Something with them...
}
临风闻羌笛 2024-10-03 12:07:22

或者,如果您使用 PHP >= 5.3:

array_walk(
        range(1,9),//<== normally your array, range(1,9) is just an example/test value
        function($value,$key) use (&$return){
                 $return[$key % 3][] = $value;
        });
var_dump($return);

Alternatively, if you use PHP >= 5.3:

array_walk(
        range(1,9),//<== normally your array, range(1,9) is just an example/test value
        function($value,$key) use (&$return){
                 $return[$key % 3][] = $value;
        });
var_dump($return);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文