3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input1 = [ '0' => [ 'foo01' => 'value01', 'bar02' => 'value02', 'formats01' => [ 'subfoo_011' => 'subvalue_011', 'subfoo_012' => 'subvalue_012', ] ], '1' => [ 'foo11' => 'value11', 'bar12' => 'value12', 'formats02' => [ 'subfoo_021' => 'subvalue_021', 'subfoo_022' => 'subvalue_022', ] ] ]; $input2 = [ 'foo' => 'value1', 'bar' => 'value2', 'formats' => [ 'foo' => 'value_1', 'bar' => 'value_2', ] ]; function countValues(array $array) { $i = 0; foreach ($array as $arrayKey => $data) { $i++; $ii = 0; foreach ($data as $key => $value) { $ii++; if (!is_array($value)) { $newArray[':' . $key . '_' . $i . '_' . $ii] = $value; } else if (is_array($value)) { foreach ($value as $subKey => $subValue) { $newArray[':' . $subKey . '_' . $i . '_' . $ii] = $subValue; } } } } return $newArray; } var_dump(countValues($input1)); var_dump(countValues([$input2]));
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.24, 8.3.0 - 8.3.12
array(8) { [":foo01_1_1"]=> string(7) "value01" [":bar02_1_2"]=> string(7) "value02" [":subfoo_011_1_3"]=> string(12) "subvalue_011" [":subfoo_012_1_3"]=> string(12) "subvalue_012" [":foo11_2_1"]=> string(7) "value11" [":bar12_2_2"]=> string(7) "value12" [":subfoo_021_2_3"]=> string(12) "subvalue_021" [":subfoo_022_2_3"]=> string(12) "subvalue_022" } array(4) { [":foo_1_1"]=> string(6) "value1" [":bar_1_2"]=> string(6) "value2" [":foo_1_3"]=> string(7) "value_1" [":bar_1_3"]=> string(7) "value_2" }

preferences:
83.81 ms | 409 KiB | 5 Q