解析逗号分隔的字符串,将数组拆分为块,然后转置
Parse comma separated string, split array into chunks, then transpose
回显值分组的数组的最佳方法是什么:
Array (
[0] => Service, Service, Service
[1] => 27 february, 2017, 27 march, 2017, 27 april, 2017
[2] => 08:00, 08:00, 08:00
)
如果我希望结果是:
Service, 27 february, 2017 at 08:00.
Service, 27 march, 2017 at 08:00.
Service, 27 april, 2017 at 08:00.
这是我到目前为止想出的:
<?php
$string = "Service, Service, Service, 8 mars, 2017, 22 mars, 2017, 5 april, 2017, 08:00, 08:00, 08:00";
$pattern = '/(\d+) (\w+), (\d+)/i'; //remove comma before year
$replacement = ' ';
$fixed = preg_replace($pattern, $replacement, $string);
$array = explode(', ', $fixed);
$i=0;
foreach($array as $value) {
echo $value.", ";
$i++;
if ($i == 3) {
echo '<br>';
$i = 0;
}
}
?>
输出:
Service, Service, Service,
8 mars 2017, 22 mars 2017, 5 april 2017,
08:00, 08:00, 08:00,
所以它们的顺序仍然错误...我只是不知道如何对数组进行排序并将我想要的值一个接一个地分组而不是连续分组。
给这只猫剥皮的方法有很多种。
我已努力提供一个灵活的解决方案,您(或其他任何人)可以简单地修改if/when您的字符串包含多于或少于 3 行的值
$string="Service, Service, Service, 8 mars, 2017, 22 mars, 2017, 5 april, 2017, 08:00, 08:00, 08:00";
$string=preg_replace('/(\d{1,2}\s\w+),(\s\d{4})/','',$string);
/* "Service, Service, Service, 8 mars 2017, 22 mars 2017, 5 april 2017, 08:00, 08:00, 08:00" */
$array=explode(', ',$string); // divide at comma&space's
/* array (
0 => 'Service',
1 => 'Service',
2 => 'Service',
3 => '8 mars 2017',
4 => '22 mars 2017',
5 => '5 april 2017',
6 => '08:00',
7 => '08:00',
8 => '08:00'
) */
$rows=3; // change this if your original string holds > or < 3 rows
$array=array_chunk($array,$rows); // group elements into specified number of rows
/* array (
0 => array (
0 => 'Service',
1 => 'Service',
2 => 'Service'
),
1 => array (
0 => '8 mars 2017',
1 => '22 mars 2017',
2 => '5 april 2017'
),
2 => array (
0 => '08:00',
1 => '08:00',
2 => '08:00'
)
) */
// loop indexes of each element in the first sub-array (group/chunk)
foreach(array_keys($array[0]) as $key){
// access each value in all sub-arrays with same key/index (column)
/* column 0 holds:
array (
0 => 'Service',
1 => '8 mars 2017',
2 => '08:00'
) */
// then join them together using a space as glue
// and push into the final array
$result[]=implode(' ',array_column($array,$key));
}
/* result holds:
array (
0 => 'Service 8 mars 2017 08:00',
1 => 'Service 22 mars 2017 08:00',
2 => 'Service 5 april 2017 08:00'
)
对于喜欢紧凑(几乎不可读)函数调用的任何人,您可以:
function davids_WP_workaround($string,$rows){
$array=array_chunk(explode(', ',preg_replace('/(\d{1,2}\s\w+),(\s\d{4})/','',$string)),$rows); // group 9 elements into sets of 3 elements
foreach(array_keys($array[0]) as $key){
$result[]=implode(' ',array_column($array,$key));
}
return $result;
}
$string="Service, Service, Service, 8 mars, 2017, 22 mars, 2017, 5 april, 2017, 08:00, 08:00, 08:00";
echo "<pre>";
var_export(davids_WP_workaround($string,3));
echo "</pre>";
回显值分组的数组的最佳方法是什么:
Array (
[0] => Service, Service, Service
[1] => 27 february, 2017, 27 march, 2017, 27 april, 2017
[2] => 08:00, 08:00, 08:00
)
如果我希望结果是:
Service, 27 february, 2017 at 08:00.
Service, 27 march, 2017 at 08:00.
Service, 27 april, 2017 at 08:00.
这是我到目前为止想出的:
<?php
$string = "Service, Service, Service, 8 mars, 2017, 22 mars, 2017, 5 april, 2017, 08:00, 08:00, 08:00";
$pattern = '/(\d+) (\w+), (\d+)/i'; //remove comma before year
$replacement = ' ';
$fixed = preg_replace($pattern, $replacement, $string);
$array = explode(', ', $fixed);
$i=0;
foreach($array as $value) {
echo $value.", ";
$i++;
if ($i == 3) {
echo '<br>';
$i = 0;
}
}
?>
输出:
Service, Service, Service,
8 mars 2017, 22 mars 2017, 5 april 2017,
08:00, 08:00, 08:00,
所以它们的顺序仍然错误...我只是不知道如何对数组进行排序并将我想要的值一个接一个地分组而不是连续分组。
给这只猫剥皮的方法有很多种。
我已努力提供一个灵活的解决方案,您(或其他任何人)可以简单地修改if/when您的字符串包含多于或少于 3 行的值
$string="Service, Service, Service, 8 mars, 2017, 22 mars, 2017, 5 april, 2017, 08:00, 08:00, 08:00";
$string=preg_replace('/(\d{1,2}\s\w+),(\s\d{4})/','',$string);
/* "Service, Service, Service, 8 mars 2017, 22 mars 2017, 5 april 2017, 08:00, 08:00, 08:00" */
$array=explode(', ',$string); // divide at comma&space's
/* array (
0 => 'Service',
1 => 'Service',
2 => 'Service',
3 => '8 mars 2017',
4 => '22 mars 2017',
5 => '5 april 2017',
6 => '08:00',
7 => '08:00',
8 => '08:00'
) */
$rows=3; // change this if your original string holds > or < 3 rows
$array=array_chunk($array,$rows); // group elements into specified number of rows
/* array (
0 => array (
0 => 'Service',
1 => 'Service',
2 => 'Service'
),
1 => array (
0 => '8 mars 2017',
1 => '22 mars 2017',
2 => '5 april 2017'
),
2 => array (
0 => '08:00',
1 => '08:00',
2 => '08:00'
)
) */
// loop indexes of each element in the first sub-array (group/chunk)
foreach(array_keys($array[0]) as $key){
// access each value in all sub-arrays with same key/index (column)
/* column 0 holds:
array (
0 => 'Service',
1 => '8 mars 2017',
2 => '08:00'
) */
// then join them together using a space as glue
// and push into the final array
$result[]=implode(' ',array_column($array,$key));
}
/* result holds:
array (
0 => 'Service 8 mars 2017 08:00',
1 => 'Service 22 mars 2017 08:00',
2 => 'Service 5 april 2017 08:00'
)
对于喜欢紧凑(几乎不可读)函数调用的任何人,您可以:
function davids_WP_workaround($string,$rows){
$array=array_chunk(explode(', ',preg_replace('/(\d{1,2}\s\w+),(\s\d{4})/','',$string)),$rows); // group 9 elements into sets of 3 elements
foreach(array_keys($array[0]) as $key){
$result[]=implode(' ',array_column($array,$key));
}
return $result;
}
$string="Service, Service, Service, 8 mars, 2017, 22 mars, 2017, 5 april, 2017, 08:00, 08:00, 08:00";
echo "<pre>";
var_export(davids_WP_workaround($string,3));
echo "</pre>";