<?php
function format_array($pattern, array $array) {
return array_map(
function (array $data) use ($pattern) {
return preg_replace_callback(
'/\{(\w+)\}/',
function (array $match) use ($data) { return $data[$match[1]]; },
$pattern
);
},
$array
);
}
$array = array(
1 => array(
'quote' => 'To be or not to be',
'citation' => 'Hamlet'
),
2 => array(
'quote' => 'The left hand knows that the right hand is doing',
'citation' => 'President Bush'
),
3 => array(
'quote' => 'Another quote',
'citation' => 'Another person'
)
);
$array = format_array('{quote} – {citation}', $array);
print_r($array);
preferences:
41.29 ms | 402 KiB | 5 Q