从两个阵列打印信息

时间:2012-11-16 14:14:29

标签: php arrays foreach

现在尝试从数组中打印信息,并且不想在foreach循环中使用manu foreach。如此有趣的是如何从这个数组输出数据

Array
(
[aM] => Array
    (
        [0] => Array
            (
                [iId] => 0
                [iTime] => 00
            )

        [1] => Array
            (
                [iId] => 1
                [iTime] => 11
            )

        [2] => Array
            (
                [iId] => 2
                [iTime] => 22
            )
    )

[aN] => Array
    (
        [0] => Array
            (
                [sName] => a
            )

        [1] => Array
            (
                [sName] => b
            )

        [2] => Array
            (
                [sName] => v
            )
    )
)

像这样,第一个数据的数据靠近第二个数据

0 a, 1 b, 2v

?不像这里,首先我们输出所有数据,而不是第二个

0 1 2 a b v

2 个答案:

答案 0 :(得分:4)

您可以使用MultipleIterator轻松完成此操作,这将轻松迭代两个数组:

// $array = your array from up there

$iter = new MultipleIterator;
$iter->attachIterator( new ArrayIterator( $array['aM']));
$iter->attachIterator( new ArrayIterator( $array['aN']));

foreach( $iter as $data) {
    list( $a, $b) = $data;
    echo $a['iId'] . ' ' . $b['sName'] . ',';
}

你可以从this demo看到它打印出来(对于PHP> = 5.3):

0 a,1 b,2 v,

答案 1 :(得分:1)

如果我找对你,你想迭代'aM',然后在'aN'中找到相应的项目。

$sets = array();
foreach ($arr['aM'] as $key => $item) {
    $sets[] = $item['iId'] . ' ' . $arr['aN'][$key]['sName'];
}
echo join(', ', $sets);