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.
Result of php executing
Full code of array_combine_1.php
- <?php
- function array_combine1(array $array1, array $array2): array
- {
- $arr1Count = \count($array1);
- $arr2Count = \count($array2);
- if ($arr1Count > $arr2Count) {
- for ($i = 0; $i < \count($array1); ++$i) {
- isset($array2[$i]) ?: $array2[$i] = null;
- }
- } elseif ($arr1Count < $arr2Count) {
- $unexpectedCount = 0;
- for ($i = 0; $i < $arr2Count; ++$i) {
- if (!isset($array1[$i])) {
- unset($array2[$i]);
- ++$unexpectedCount;
- }
- }
- $array1[] = 'UNEXPECTED_FIELDS_COUNT';
- $array2[] = $unexpectedCount;
- }
- return array_combine($array1, $array2);
- }
- var_dump(array_combine1(
- ['test_header_1', 'test_header_2'],
- ['test_value_1','test_value_2','test_value_3', 'test_value_4']
- ));
- var_dump(array_combine1(
- ['test_header_1', 'test_header_2', 'test_header_3'],
- ['test_value_1']
- ));
File Description
- array_combine_1
- PHP Code
- 21 Jun-2019
- 1.05 Kb
You can Share it:
Latest PHP Pastes