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]));

preferences:
22.48 ms | 404 KiB | 5 Q