array_combine_1 - 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_1.php

  1. <?php
  2.  
  3.     function array_combine1(array $array1, array $array2): array
  4.     {
  5.         $arr1Count = \count($array1);
  6.         $arr2Count = \count($array2);
  7.  
  8.         if ($arr1Count > $arr2Count) {
  9.             for ($i = 0; $i < \count($array1); ++$i) {
  10.                 isset($array2[$i]) ?: $array2[$i] = null;
  11.             }
  12.         } elseif ($arr1Count < $arr2Count) {
  13.             $unexpectedCount = 0;
  14.             for ($i = 0; $i < $arr2Count; ++$i) {
  15.                 if (!isset($array1[$i])) {
  16.                     unset($array2[$i]);
  17.                     ++$unexpectedCount;
  18.                 }
  19.             }
  20.             $array1[] = 'UNEXPECTED_FIELDS_COUNT';
  21.             $array2[] = $unexpectedCount;
  22.         }
  23.  
  24.         return array_combine($array1, $array2);
  25.     }
  26.  
  27.     var_dump(array_combine1(
  28.             ['test_header_1', 'test_header_2'],
  29.             ['test_value_1','test_value_2','test_value_3', 'test_value_4']
  30.         ));
  31.     var_dump(array_combine1(
  32.             ['test_header_1', 'test_header_2', 'test_header_3'],
  33.             ['test_value_1']
  34.         ));
  35.         
File Description
  • array_combine_1
  • PHP Code
  • 21 Jun-2019
  • 1.05 Kb
You can Share it: