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);

preferences:
29.4 ms | 405 KiB | 5 Q