settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Zig-Zag Arrays - PHP Array Task

+2 votes

Write a program which creates 2 arrays. You will be given an integer n. On the next n lines you get 2 integers.

Form 2 arrays as shown below:

zig zag arrays php array task

asked in PHP category by user hues

1 Answer

+1 vote

My solution:

<?php

$number = intval(readline());
$arrZig = [];
$arrZag = [];

for ($i = 0; $i < $number; $i++) {

    $args = explode(' ', readline());

    if ($i % 2 == 0) {
        $arrZig[] = $args[0];
        $arrZag[] = $args[1];
    } else {
        $arrZig[] = $args[1];
        $arrZag[] = $args[0];
    }
}

echo implode(' ', $arrZig) . PHP_EOL;
echo implode(' ', $arrZag);
answered by user icabe
...