PHP数组中的数组迭代

PHP数组中的数组迭代,php,arrays,for-loop,Php,Arrays,For Loop,如何对该数组进行itarate array(2) { [0]=> array(1) { [0]=> array(14) { [0]=> string(17) "ratosk8@censored" [1]=> string(23) "alokkumar.censored" [2]=> string(24) "uuleticialima1@censored" [3

如何对该数组进行itarate

array(2) {
  [0]=>
  array(1) {
    [0]=>
    array(14) {
      [0]=>
      string(17) "ratosk8@censored"
      [1]=>
      string(23) "alokkumar.censored"
      [2]=>
      string(24) "uuleticialima1@censored"
      [3]=>
      string(23) "camera.clicks@censored"
      [4]=>
      string(24) "billthailand@censored"
      [5]=>
      string(17) "v.golev@censored"
      [6]=>
      string(22) "flipe.lost@censored"
      [7]=>
      string(25) "notherdirtybird@censored"
      [8]=>
      string(21) "booktiphani@censored"
      [9]=>
      string(32) "opinion?jazzantoledo@censored"
      [10]=>
      string(25) "skateforemerica@censored"
      [11]=>
      string(28) "blockdymezmagazine@censored"
      [12]=>
      string(17) "and6451@censored"
      [13]=>
      string(22) "flipe.lost@censored"
    }
  }
  [1]=>
  array(1) {
    [0]=>
    array(14) {
      [0]=>
      string(17) "ratosk8@censored"
      [1]=>
      string(23) "alokkumar.jsr@censored"
      [2]=>
      string(24) "uuleticialima1@censored.com"
      [3]=>
      string(23) "camera.clicks@censored.com"
      [4]=>
      string(24) "billthailand@censored.com"
      [5]=>
      string(17) "v.golev@censored.com"
      [6]=>
      string(22) "flipe.lost@censored.com"
      [7]=>
      string(25) "notherdirtybird@censored.com"
      [8]=>
      string(21) "booktiphani@censored.com"
      [9]=>
      string(32) "opinion?jazzantoledo@censored.com"
      [10]=>
      string(25) "skateforemerica@censored.com"
      [11]=>
      string(28) "blockdymezmagazine@censored.com"
      [12]=>
      string(17) "and6451@censored.com"
      [13]=>
      string(22) "flipe.lost@censored.com"
    }
  }
}

您可以在另一个
foreach
中使用
foreach

foreach ($array as $item) {
    foreach ($item[0] as $email) {
        // …
    }
}
function array_flatten($array) {
    if (!is_array($array)) {
        return false;
    }
    $result = array();
    $stack = $array;
    $len = count($stack);
    while ($len) {
        $val = array_shift($stack);
        --$len;
        if (is_array($val)) {
            foreach ($val as $key => $val) {
                if (is_array($val)) {
                    array_unshift($stack, $val);
                    ++$len;
                } else {
                    $result[] = $val;
                }
            }
        } else {
            $result[] = $val;
        }
    }
    return $result;
}
请注意,我使用了
$item[0]
,而不仅仅是
$item

您还可以使用函数展平多维数组,然后使用单个
foreach

foreach ($array as $item) {
    foreach ($item[0] as $email) {
        // …
    }
}
function array_flatten($array) {
    if (!is_array($array)) {
        return false;
    }
    $result = array();
    $stack = $array;
    $len = count($stack);
    while ($len) {
        $val = array_shift($stack);
        --$len;
        if (is_array($val)) {
            foreach ($val as $key => $val) {
                if (is_array($val)) {
                    array_unshift($stack, $val);
                    ++$len;
                } else {
                    $result[] = $val;
                }
            }
        } else {
            $result[] = $val;
        }
    }
    return $result;
}

您可以在另一个
foreach
中使用
foreach

foreach ($array as $item) {
    foreach ($item[0] as $email) {
        // …
    }
}
function array_flatten($array) {
    if (!is_array($array)) {
        return false;
    }
    $result = array();
    $stack = $array;
    $len = count($stack);
    while ($len) {
        $val = array_shift($stack);
        --$len;
        if (is_array($val)) {
            foreach ($val as $key => $val) {
                if (is_array($val)) {
                    array_unshift($stack, $val);
                    ++$len;
                } else {
                    $result[] = $val;
                }
            }
        } else {
            $result[] = $val;
        }
    }
    return $result;
}
请注意,我使用了
$item[0]
,而不仅仅是
$item

您还可以使用函数展平多维数组,然后使用单个
foreach

foreach ($array as $item) {
    foreach ($item[0] as $email) {
        // …
    }
}
function array_flatten($array) {
    if (!is_array($array)) {
        return false;
    }
    $result = array();
    $stack = $array;
    $len = count($stack);
    while ($len) {
        $val = array_shift($stack);
        --$len;
        if (is_array($val)) {
            foreach ($val as $key => $val) {
                if (is_array($val)) {
                    array_unshift($stack, $val);
                    ++$len;
                } else {
                    $result[] = $val;
                }
            }
        } else {
            $result[] = $val;
        }
    }
    return $result;
}

这里有一个两元素数组,其中每个元素都包含一个一元素数组,其中包含要迭代的数组

// This gets us the two seperate arrays
foreach ($root_array as $2nd_level_array) {

  // But now, we still have one more one-element array in each...
  // we could iterate, but since there's only one element, 
  // we can just grab that element using array_pop()
  $email_address_array = array_pop($2nd_level_array)

  // now we can iterate over each of the two lists
  foreach ($email_address_array as $email_address) {

    // process email address

  }

}

这里有一个两元素数组,其中每个元素都包含一个一元素数组,其中包含要迭代的数组

// This gets us the two seperate arrays
foreach ($root_array as $2nd_level_array) {

  // But now, we still have one more one-element array in each...
  // we could iterate, but since there's only one element, 
  // we can just grab that element using array_pop()
  $email_address_array = array_pop($2nd_level_array)

  // now we can iterate over each of the two lists
  foreach ($email_address_array as $email_address) {

    // process email address

  }

}

也许更灵活的解决方案是递归:

<?php

$a = array();
$a[] = array( array( "a", "b") );
$a[] = array( array( array("c", "d"), array("e", "f"), array("g", "h")));


print_arr($a);

function print_arr($obj) {
    foreach($obj as $k => $v) {
        if(is_array($v)) {
            print_arr($v);
        }else{
            echo $v ."<br />";
        }
    }
}

也许更灵活的解决方案是递归:

<?php

$a = array();
$a[] = array( array( "a", "b") );
$a[] = array( array( array("c", "d"), array("e", "f"), array("g", "h")));


print_arr($a);

function print_arr($obj) {
    foreach($obj as $k => $v) {
        if(is_array($v)) {
            print_arr($v);
        }else{
            echo $v ."<br />";
        }
    }
}

关于如何迭代数组,问题不是太具体,因为它是一个多维数组,有几种方法可以实现

下面我概述了我认为最有意义的两种方法

首先,我遍历数组,按顺序列出项目

在我的第二次尝试中,我遍历了数组,根据键对第二级项目进行分组

$a = array(
    0 => array(
    0 => array(
        0 => "test0-0-0",
        1 => "test0-0-1"
    )
    ),
    1 => array(
    0 => array(
        0 => "test1-0-0",
        1 => "test1-0-1"
    )
    )
);

echo "Method 1\n";
foreach ($a as $array) {
    foreach ($array[0] as $item) {
    echo "$item\n";
    }
}
echo "Method 2\n";
$a0 = $a[0][0];
$a1 = $a[1][0];
if (count($a0) == count($a1)) {
    for ($i=0;$i<count($a0);++$i) {
    echo $a0[$i]."\n";
    echo $a1[$i]."\n";
    }
}
$a=数组(
0=>数组(
0=>数组(
0=>“测试0-0-0”,
1=>“测试0-0-1”
)
),
1=>数组(
0=>数组(
0=>“测试1-0-0”,
1=>“测试1-0-1”
)
)
);
echo“方法1\n”;
foreach($a作为$array){
foreach($array[0]作为$item){
回显“$item\n”;
}
}
echo“方法2\n”;
$a0=$a[0][0];
$a1=$a[1][0];
如果(计数($a0)=计数($a1)){

对于($i=0;$i而言,关于如何迭代数组的问题并不太具体,因为它是一个多维数组,所以有几种方法可以实现

下面我概述了我认为最有意义的两种方法

首先,我遍历数组,按顺序列出项目

在我的第二次尝试中,我遍历了数组,根据键对第二级项目进行分组

$a = array(
    0 => array(
    0 => array(
        0 => "test0-0-0",
        1 => "test0-0-1"
    )
    ),
    1 => array(
    0 => array(
        0 => "test1-0-0",
        1 => "test1-0-1"
    )
    )
);

echo "Method 1\n";
foreach ($a as $array) {
    foreach ($array[0] as $item) {
    echo "$item\n";
    }
}
echo "Method 2\n";
$a0 = $a[0][0];
$a1 = $a[1][0];
if (count($a0) == count($a1)) {
    for ($i=0;$i<count($a0);++$i) {
    echo $a0[$i]."\n";
    echo $a1[$i]."\n";
    }
}
$a=数组(
0=>数组(
0=>数组(
0=>“测试0-0-0”,
1=>“测试0-0-1”
)
),
1=>数组(
0=>数组(
0=>“测试1-0-0”,
1=>“测试1-0-1”
)
)
);
echo“方法1\n”;
foreach($a作为$array){
foreach($array[0]作为$item){
回显“$item\n”;
}
}
echo“方法2\n”;
$a0=$a[0][0];
$a1=$a[1][0];
如果(计数($a0)=计数($a1)){

对于($i=0;$i我将创建一个函数并将其传递到

或者,您可以使用a在a上顺序迭代。它们的文档记录得很差,但我相信代码如下所示:

//iterate over the array using recursion as in les' answer (doesn't gain much)
$array_iter=new RecursiveArrayIterator($people_to_spam);

//Ahh, here we go, this will let us iterate over the leaves in a sequential manner
$iter_iter=new RecursiveIteratorIterator($array_iter);
foreach($iter_iter as $email)
{
    send_evil_spam($email)
}

我发现这两种解决方案是最干净、可读性最强的。如果数组只有3层深度,我可能只是硬编码。如果我不知道这两种方法中的任何一种,我只需编写自己的递归函数即可(如les的回答所示)。

我会创建一个函数并将其传递到

或者,您可以使用a在a上顺序迭代。它们的文档记录得很差,但我相信代码如下所示:

//iterate over the array using recursion as in les' answer (doesn't gain much)
$array_iter=new RecursiveArrayIterator($people_to_spam);

//Ahh, here we go, this will let us iterate over the leaves in a sequential manner
$iter_iter=new RecursiveIteratorIterator($array_iter);
foreach($iter_iter as $email)
{
    send_evil_spam($email)
}

我发现这两种解决方案是最干净、可读性最强的。如果数组只有3层深度,我可能只是硬编码。如果我不知道这两种方法中的任何一种,我只需编写自己的递归函数即可(如les的回答所示).

如果其中一封是我的电子邮件,我会非常生气……这在有人查看编辑历史记录之前很有帮助。@Chris:对–我标记了这封邮件以引起版主的注意。如果其中一封是我的电子邮件,我会非常生气……这在有人查看编辑历史记录之前很有帮助。@Chris:对–我标记了这封邮件以引起版主的注意。@danieltalsky:N请注意,我使用了
$item[0]
。请注意。对此表示抱歉。它不允许我更改我的投票……您可能需要编辑此项并使其更清晰地显示给用户submitter@danieltalsky:请注意,我使用了
$item[0]
。注意。对此表示抱歉。它不允许我更改我的投票……您可能希望对提交者进行编辑并使其更清楚