array_combine - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of array_combine.php

  1. <?php
  2. function c($h, $a) {
  3.     $r = [];
  4.     foreach($a as $i => $v) {
  5.         $k = $h[$i] ?? false;
  6.         if ($k !== false) {
  7.             $r[$k] = $v;
  8.         } else {
  9.             $r[] = $v;
  10.         }
  11.     }
  12.     return array_pad($r, count($h), null);
  13. }
  14.  
  15. $h = ['a', 'b', 'c'];
  16. $row = [4,3,2,1];
  17. var_dump(c($h,$row));
  18.  
  19. $add = array_splice($row, count($h));
  20. $res = array_combine($h, array_pad($row, count($h), null)) + $add;
  21. var_dump($res);
  22.  
  23.  
  24. $h = ['a', 'b', 'c'];
  25. $row = [4,3,2];
  26. var_dump(c($h,$row));
  27.  
  28. $add = array_splice($row, count($h));
  29. $res = array_combine($h, array_pad($row, count($h), null)) + $add; 
  30. var_dump($res);
  31.  
  32. $h = ['a', 'b', 'c'];
  33. $row = [4,3];
  34. var_dump(c($h,$row));
  35.  
  36. $add = array_splice($row, count($h));
  37. $res = array_combine($h, array_pad($row, count($h), null)) + $add;
  38. var_dump($res);
  39.  
File Description
  • array_combine
  • PHP Code
  • 21 Jun-2019
  • 805 Bytes
You can Share it: