Help us understand the problem. What is going on with this article?

【PHP】複数の配列を同時にwhileで回す

More than 3 years have passed since last update.

たまによく使うのでメモ
※foreachではなくwhileを使います

test.php
    <?php

        $array1 = array('今日','天気','晴れ','雨');
        $array2 = array('の','は','のち','です');

        while ( (list($key1, $val1) = each($array1))  
            && (list($key2, $val2) = each($array2)) ) {

                echo $val1.$val2;
        }

    ?>

結果:今日の天気は晴れのち雨です

追記

@mpyw 様よりeach関数はPHP8で消えるとの情報を頂き、
PHP7.1以降では下記コードが推奨となります。

test.php
<?php

$array1 = ['今日', '天気', '晴れ', '雨'];
$array2 = ['の', 'は', 'のち', 'です'];

foreach (array_map(null, $array1, $array2) as [$val1, $val2]) {
    echo "$val1$val2";
}
KOH_TA
php / js / objective-c / swift
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away