3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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]));
based on gJkZa
Output for 5.6.30, hhvm-3.18.5 - 3.22.0, 7.0.28 - 7.3.0rc3
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" }