lithium\util\Set::format()
Returns a series of values extracted from an array, formatted in a format string.
Parameters
-
array
$data
Source array from which to extract the data.
-
string
$format
Format string into which values will be inserted using
sprintf()
. -
array
$keys
An array containing one or more
Set::extract()
-style key paths.
Returns
arrayAn array of strings extracted from $keys
and formatted with $format
.
Links
Source
public static function format($data, $format, $keys) {
$extracted = [];
$count = count($keys);
if (!$count) {
return;
}
for ($i = 0; $i < $count; $i++) {
$extracted[] = static::extract($data, $keys[$i]);
}
$out = [];
$data = $extracted;
$count = count($data[0]);
if (preg_match_all('/\{([0-9]+)\}/msi', $format, $keys2) && isset($keys2[1])) {
$keys = $keys2[1];
$format = preg_split('/\{([0-9]+)\}/msi', $format);
$count2 = count($format);
for ($j = 0; $j < $count; $j++) {
$formatted = '';
for ($i = 0; $i <= $count2; $i++) {
if (isset($format[$i])) {
$formatted .= $format[$i];
}
if (isset($keys[$i]) && isset($data[$keys[$i]][$j])) {
$formatted .= $data[$keys[$i]][$j];
}
}
$out[] = $formatted;
}
return $out;
}
$count2 = count($data);
for ($j = 0; $j < $count; $j++) {
$args = [];
for ($i = 0; $i < $count2; $i++) {
if (isset($data[$i][$j])) {
$args[] = $data[$i][$j];
}
}
$out[] = vsprintf($format, $args);
}
return $out;
}