English 中文(简体)
格式数据
原标题:Format data nicely
  • 时间:2011-10-17 19:11:09
  •  标签:
  • php
  • arrays

我从APIC获得的数据,其格式相当难以使用(在需要使用的门槛值中),我没有能力修改APIC。 以下是我提供的数据:

    $data = array(
        array( day  =>  Monday ,  start  => 1100,  end  => 1300),
        array( day  =>  Tuesday ,  start  => 1100,  end  => 1300),
        array( day  =>  Wednesday ,  start  => 1100,  end  => 1300),
        array( day  =>  Thursday ,  start  => 1200,  end  => 1300),
        array( day  =>  Friday ,  start  => 1200,  end  => 1300),
        array( day  =>  Saturday ,  start  => 1200,  end  => 1300),
        array( day  =>  Sunday ,  start  => 1200,  end  => 1400)
    );

数据可能不包括每周7天、开始和结束时间在0到24之间。 我正设法以下列产出的形式编排数据:Monday - 星期三 (1100-1300)、星期四-星期六(1200-1300)、星期日(1200-1400)基本上毗连日(开始和结束时间相同)由斜体分开。

我试图避免一幅 code强的法典。

最佳回答

Ugliness is in the context of the beholder. 我认为,这是大韩民国的,但当然,这可能是你重新努力避免的。

<?php
$data = array(
    array( day  =>  Monday ,  start  => 1100,  end  => 1300),
    array( day  =>  Tuesday ,  start  => 1100,  end  => 1300),
    array( day  =>  Wednesday ,  start  => 1100,  end  => 1300),
    array( day  =>  Thursday ,  start  => 1200,  end  => 1300),
    array( day  =>  Friday ,  start  => 1200,  end  => 1300),
    array( day  =>  Saturday ,  start  => 1200,  end  => 1300),
    array( day  =>  Sunday ,  start  => 1200,  end  => 1400)
);

$periods = array();

$start = $end = $i = -1;

foreach ($data as $datum) {
    if ($start != $datum[ start ] || $end != $datum[ end ]) {
        $i++;
        $start = $datum[ start ];
        $end   = $datum[ end ];

        $periods[$i] = array(
                           start     => $start, 
                           end       => $end,
                           startDay  => $datum[ day ]);
    }
    $periods[$i][ endDay ] = $datum[ day ];
}

foreach ($periods as $k => $period) {
    echo ($k) ?  ,   :   ;
    if ($period[ startDay ] === $period[ endDay ]) {
        echo $period[ startDay ];
    } else {
        echo "{$period[ startDay ]} - {$period[ endDay ]}";
    }
    echo " ({$period[ start ]} - {$period[ end ]})";
}

产出:

Monday - Wednesday (1100 - 1300), Thursday - Saturday (1200 - 1300), Sunday (1200 - 1400)
问题回答

我的解决办法:

function data_to_array(&$data, $cols) {
  foreach($data as &$value) {
    $value = array_combine($cols, explode( , , $value));
  }
}

$data = Array(
   Monday,1200,1400 ,
   Monday,1200,1400 ,
   Monday,1200,1400 ,
   Monday,1200,1400 ,
   Monday,1200,1400 ,
   Monday,1200,1400 ,
   Monday,1200,1400 ,
   Monday,1200,1400 ,
   Monday,1200,1400 ,
   Monday,1200,1400 ,
   Monday,1200,1400 
);

data_to_array($data, Array( day , start , end ));
// now $data is again ugly block of data :)

now you can keep it in text file data.txt:

Monday,1200,1400
Monday,1200,1400
Monday,1200,1400
Monday,1200,1400
Monday,1200,1400
Monday,1200,1400
Monday,1200,1400
Monday,1200,1400
Monday,1200,1400
Monday,1200,1400
Monday,1200,1400

<>strong>your php file:

$data = explode("
", file_get_contents( data.txt ));
data_to_array($data, Array( day , start , end ));
// now $data is again ugly block of data :)




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签