3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function hasColor(Array $a,$color){ foreach($a as $k=>$v){ if($v['color']===$color){ return true; } } return false; } function checkColors(Array &$a){ foreach($a as $k=>&$v){ switch($v['color']){ case 'blue': $appendColor = 'pink'; break; case 'green': $appendColor = 'blue'; break; case 'pink': $appendColor = 'green'; break; } if(hasColor($a,$appendColor)){ continue; } $a[] = ['color'=>$appendColor]; return checkColors($a); } } $colors = [ [ 'color'=>'blue' ] ]; checkColors($colors); var_dump($colors);
based on 5jWRX
Output for 5.6.0 - 5.6.30, hhvm-3.18.5 - 3.22.0, 7.0.0 - 7.2.6
array(3) { [0]=> array(1) { ["color"]=> string(4) "blue" } [1]=> array(1) { ["color"]=> string(4) "pink" } [2]=> array(1) { ["color"]=> string(5) "green" } }