3v4l.org

run code in 300+ PHP versions simultaneously
<?php function flatten($input_data, $path = "", $delimiter = ".", &$output = null) { if (is_null($output)) { $output = []; } if (is_array($input_data)) { if (isAssoc($input_data)) { array_push($output, [$path, new stdClass()]); foreach ($input_data as $key => $value) { if (!is_string($key)) { throw new Exception("Dictionary key must be a string, found " . gettype($key) . " instead."); } $new_key = $path ? $path . $delimiter . $key : $key; if (strpos($key, $delimiter) !== false) { throw new Exception("Key '{$key}' contains the delimiter '{$delimiter}'."); } flatten($value, $new_key, $delimiter, $output); } } else { array_push($output, [$path, []]); foreach ($input_data as $i => $value) { $new_key = $path ? $path . $delimiter . $i : strval($i); flatten($value, $new_key, $delimiter, $output); } } } else { array_push($output, [$path, $input_data]); } return $output; } function isAssoc(array $arr) { if (array() === $arr) return false; return array_keys($arr) !== range(0, count($arr) - 1); } function unflatten($array_data, $delimiter = '.') { $root = $array_data[0][1]; if (!is_array($root) && !is_object($root)) { return $root; } array_shift($array_data); foreach ($array_data as [$path, $value]) { $path_parts = explode($delimiter, $path); $target = &$root; foreach ($path_parts as $part) { if (is_numeric($part)) { $part = intval($part); } if (is_array($target)) { $target = &$target[$part]; } elseif (is_object($target)) { $target = &$target->$part; } else { // Handle error } } $target = $value; } return $root; } function test_flatten_unflatten() { $test_cases = [ [ 'input' => ['name' => 'John', 'age' => 30], 'expected_flatten' => [ ['', new stdClass()], ['name', 'John'], ['age', 30] ] ], [ 'input' => ['animals' => ['dog', 'cat']], 'expected_flatten' => [ ['', new stdClass()], ['animals', []], ['animals.0', 'dog'], ['animals.1', 'cat'] ] ], [ 'input' => 'hello', 'expected_flatten' => [ ['', 'hello'] ] ], [ 'input' => [], 'expected_flatten' => [ ['', []] ] ], [ 'input' => new stdClass(), 'expected_flatten' => [ ['', new stdClass()] ] ] ]; foreach ($test_cases as $i => $test_case) { $input = $test_case['input']; $expected_flatten = $test_case['expected_flatten']; $flattened = flatten($input); if ($flattened === $expected_flatten) { echo "Test case $i for flatten: Passed\n"; } else { echo "Test case $i for flatten: Failed\n"; print_r($flattened); } $unflattened = unflatten($flattened); if ($unflattened == $input) { echo "Test case $i for unflatten: Passed\n"; } else { echo "Test case $i for unflatten: Failed\n"; print_r($unflattened); } } } // Run the test test_flatten_unflatten(); ?>
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0 - 8.5.1
Test case 0 for flatten: Failed Array ( [0] => Array ( [0] => [1] => stdClass Object ( ) ) [1] => Array ( [0] => name [1] => John ) [2] => Array ( [0] => age [1] => 30 ) ) Test case 0 for unflatten: Failed stdClass Object ( [name] => John [age] => 30 ) Test case 1 for flatten: Failed Array ( [0] => Array ( [0] => [1] => stdClass Object ( ) ) [1] => Array ( [0] => animals [1] => Array ( ) ) [2] => Array ( [0] => animals.0 [1] => dog ) [3] => Array ( [0] => animals.1 [1] => cat ) ) Test case 1 for unflatten: Failed stdClass Object ( [animals] => Array ( [0] => dog [1] => cat ) ) Test case 2 for flatten: Passed Test case 2 for unflatten: Passed Test case 3 for flatten: Passed Test case 3 for unflatten: Passed Test case 4 for flatten: Failed Array ( [0] => Array ( [0] => [1] => stdClass Object ( ) ) ) Test case 4 for unflatten: Passed
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
125.67 ms | 408 KiB | 5 Q