Littlebobbydroptables
Littlebobbydroptables

Reputation: 3731

Print info from two arrays

Trying to print infomation from array now, and dont want to use manu foreach in foreach cycles. So interesting how to output data from this array

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
            )
    )
)

Like this, data from first array near data from second

0 a, 1 b, 2v

? Not like here, first we output all data from first, than from second

0 1 2 a b v

?

Upvotes: 0

Views: 76

Answers (2)

Ja͢ck
Ja͢ck

Reputation: 173642

If I get you right, you want to iterate over 'aM' and then find the respective item in 'aN'.

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

Upvotes: 1

nickb
nickb

Reputation: 59709

You can easily do this with a MultipleIterator, which will iterate over both arrays easily:

// $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'] . ',';
}

You can see from this demo that it prints (for PHP >= 5.3):

0 a,1 b,2 v,

Upvotes: 4

Related Questions