3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = [ ['mango', 'orange', 'apple'], ['abc'], ['ctp'], ['pqr', 'cst', 'dtc'], ['cbx'], ['xyz'], ['atc'], ]; foreach($data as $key => $d){ if(count($d)>1){ $mergeTo = $key; // store key where to merge to continue; // go to next item } $data[$mergeTo]["additional"][] = $d; // add value to $mergeTo unset($data[$key]); // unset the value from the main array } $data = array_values($data); // reset keys var_export($data);
Output for 7.1.25 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
array ( 0 => array ( 0 => 'mango', 1 => 'orange', 2 => 'apple', 'additional' => array ( 0 => array ( 0 => 'abc', ), 1 => array ( 0 => 'ctp', ), ), ), 1 => array ( 0 => 'pqr', 1 => 'cst', 2 => 'dtc', 'additional' => array ( 0 => array ( 0 => 'cbx', ), 1 => array ( 0 => 'xyz', ), 2 => array ( 0 => 'atc', ), ), ), )
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 array ( 0 => array ( 0 => 'mango', 1 => 'orange', 2 => 'apple', 'additional' => array ( 0 => array ( 0 => 'abc', ), 1 => array ( 0 => 'ctp', ), ), ), 1 => array ( 0 => 'pqr', 1 => 'cst', 2 => 'dtc', 'additional' => array ( 0 => array ( 0 => 'cbx', ), 1 => array ( 0 => 'xyz', ), 2 => array ( 0 => 'atc', ), ), ), )

preferences:
160.3 ms | 403 KiB | 183 Q