- json_decode: documentation ( source)
- preg_replace_callback: documentation ( source)
- var_export: documentation ( source)
- explode: documentation ( source)
- json_encode: documentation ( source)
- sprintf: documentation ( source)
<?php
$array = [1,1,1,2,2,3,3,4,1,1,2,2,3];
var_export(
json_decode(
preg_replace_callback(
'/\b(\d+)(?:,\1)*\b/',
function($m) {
return sprintf(
'{"%d":%d}',
$m[1],
count(explode(',', $m[0]))
);
},
json_encode($array)
),
true
)
);