Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.73 KB | None | 0 0
  1. <?php
  2.  
  3. // bench
  4. //$t = microtime(true);
  5. //$array = array();
  6. //for($i = 0; $i < 1000000; $i++) {
  7. //  $array[] = $i;
  8. //}
  9. //print microtime(true) - $t;
  10. //print "\n";
  11. //$t = microtime(true);
  12. //$array = array();
  13. //for($i = 0; $i < 1000000; $i++) {
  14. //  array_push($array, $i);
  15. //}
  16. //print microtime(true) - $t;
  17.  
  18. $a = [
  19.     'tt',
  20.     'tttt',
  21.     [
  22.         'asd',
  23.         'dsa',
  24.     ],
  25.     'ddd',
  26.     'bbb'
  27. ];
  28.  
  29. print_r($a);
  30.  
  31. $new = [];
  32. foreach($a as $val) {
  33.     if ( ! is_array( $val ) ) {
  34.         $new[] = $val;
  35.         continue;
  36.     }
  37.  
  38. //  можно и так, но это медленнее
  39. //  array_push( $new, ...$val );
  40.  
  41.     // Такой вариант быстрее в среднем на 50-70%
  42.     foreach($val as $v) {
  43.         $new[] = $v;
  44.     }
  45. }
  46.  
  47. print_r($new);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement