3v4l.org

run code in 300+ PHP versions simultaneously
<?php $ar = []; $i = 0; while($i < 2){ $ar['fluits'][$i][] = [ 'name' => 'apple', 'color' => 'red' ]; $ar['fluits'][$i][] = [ 'name' => 'kiwi', 'color' => 'green' ]; $ar['drinks'][$i][] = [ 'name' => 'coffee', 'taste' => 'bitter' ]; // これだと「apple」の配列には「2」が追加されるが、kiwiの配列に「2」が追加されない。 $add = 'on'; if( $add == 'on' ){ $ar['drinks'][$i][0]['id'] = '1'; $ar['fluits'][$i][0]['id'] = '2'; } $i++; } var_dump($ar);
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.24, 7.4.0 - 7.4.12, 8.0.0
array(2) { ["fluits"]=> array(2) { [0]=> array(2) { [0]=> array(3) { ["name"]=> string(5) "apple" ["color"]=> string(3) "red" ["id"]=> string(1) "2" } [1]=> array(2) { ["name"]=> string(4) "kiwi" ["color"]=> string(5) "green" } } [1]=> array(2) { [0]=> array(3) { ["name"]=> string(5) "apple" ["color"]=> string(3) "red" ["id"]=> string(1) "2" } [1]=> array(2) { ["name"]=> string(4) "kiwi" ["color"]=> string(5) "green" } } } ["drinks"]=> array(2) { [0]=> array(1) { [0]=> array(3) { ["name"]=> string(6) "coffee" ["taste"]=> string(6) "bitter" ["id"]=> string(1) "1" } } [1]=> array(1) { [0]=> array(3) { ["name"]=> string(6) "coffee" ["taste"]=> string(6) "bitter" ["id"]=> string(1) "1" } } } }