[Löst] Zig-Zag Arrays - PHP Array Task

  

3
Ämnesstart

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

1 Answer
2

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);
Dela: