duanlu1922 2017-02-26 15:42
浏览 49
已采纳

具有存储在行中的值的Echo数组

What is the best way to echo out an array where the values are grouped:

Array (
[0] => Service, Service, Service
[1] => 27 february, 2017, 27 march, 2017, 27 april, 2017
[2] => 08:00, 08:00, 08:00
)

If i want the result to be:

Service, 27 february, 2017 at 08:00.

Service, 27 march, 2017 at 08:00.

Service, 27 april, 2017 at 08:00.

This is what i have come up with so far:

<?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 = '${1} $2 $3';
    $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;
        }
    }

?>

The output:

Service, Service, Service,

8 mars 2017, 22 mars 2017, 5 april 2017,

08:00, 08:00, 08:00,

So they are still in the wrong order... I just cant figure out how to sort the array and group the values i want one after another insted of in a row.

The sorting Arrays functions doesn´t work for this. Got any ideas?

  • 写回答

1条回答 默认 最新

  • dragon321723 2017-03-10 12:39
    关注

    There will be many ways to skin this cat.

    I have endeavored to provide a flexible solution that you (or anyone else) can simply modify if/when your string contains more or less than 3 rows of values

    $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})/','$1$2',$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'
            )
    

    For anyone who prefers a compact (and nearly unreadable) function to call, here you go:

    function davids_WP_workaround($string,$rows){
        $array=array_chunk(explode(', ',preg_replace('/(\d{1,2}\s\w+),(\s\d{4})/','$1$2',$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>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 如果update 一个列名为参数的value
  • ¥15 基于51单片机的水位检测系统设计中LCD1602一直不显示
  • ¥15 OCS2安装出现问题,请大家给点意见
  • ¥15 ros小车启动launch文件报错
  • ¥15 vs2015到期想登陆但是登陆不上
  • ¥15 IPQ5018制作烧录固件,boot运行失败(操作系统-linux)(相关搜索:操作系统)(相关搜索:操作系统)
  • ¥20 icefall在librispeech基础上加入个人数据集
  • ¥30 keepalive高可用故障运维配置询问
  • ¥15 求帮助!国家电网内网u盘突然识别不出来了。
  • ¥15 matlab语音变速变调同时实现