1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

phpでの配列結合

Posted at

phpで配列同士を結合した時の挙動。
勘違いしてたので、メモ用に。

sumArray.php
<?php

    // 配列$x,$y宣言
    $x[0] = 1;
    $x[1] = 5;
    $x[2] = 10; 
    $x[3] = 20; 

    $y[2] = 3;
    $y[4] = 22; 

    // $xと$yを結合
    $z = $x + $y; 

    // 表示
    foreach($z as $key => $value){
            echo '$z['.$key.'] => '. $value . "\n";
    }   
?>


//出力結果
$z[0] => 1
$z[1] => 5
$z[2] => 10
$z[3] => 20
$z[4] => 22

若干、分かりづらいけど、
要は配列同士を+したら同じ位置にある値が上書きされると思ってたけど、
実は空いてる要素にしか代入してなかったって話しでした。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?