3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php // 元データを用意する $a[2]=10; $a[10]=2; $a[3]=30; $a[22]=20; $a[9]=4; $a[53]=29; //確認 echo ($a); //キーを保持してソート asort($a); //確認 print_r($a); //最初から 3 個を $b[] に代入 $i = 0; $b = array(); foreach ($a as $k=>$d){ $b[] = $k; if (3 <= ++$i){ break;} } //確認 print_r($b); ?>
Output for 5.4.0 - 7.1.0
Notice: Array to string conversion in /in/11pqS on line 10 ArrayArray ( [10] => 2 [9] => 4 [2] => 10 [22] => 20 [53] => 29 [3] => 30 ) Array ( [0] => 10 [1] => 9 [2] => 2 )
Output for 4.3.0 - 5.3.29
ArrayArray ( [10] => 2 [9] => 4 [2] => 10 [22] => 20 [53] => 29 [3] => 30 ) Array ( [0] => 10 [1] => 9 [2] => 2 )