我正在使用PHP,并且有一个看起来像这样的数组:


Array
(
    [0] =>  class_name:10:date:2014-03-08:start_time:10:15:finish_time:12:15:subject:Maths:exam_details:Extra comments:
    [1] =>  class_name:10:date:2014-03-07:start_time:10:15:finish_time:12:15:subject:Eng:exam_details:Extra comments:
    [2] =>  class_name:10:date:2014-03-06:start_time:10:15:finish_time:12:15:subject:Science:exam_details:Extra comments:
    [3] =>  class_name:10:date:2014-03-06:start_time:10:15:finish_time:12:15:subject:Science:exam_details:Extra comments:
    [4] =>  class_name:10:date:2014-03-06:start_time:10:15:finish_time:12:15:subject:Science:exam_details:Extra comments:
    [5] =>  class_name:10:date:2014-03-06:start_time:10:15:finish_time:12:15:subject:Science:exam_details:Extra comments:
)

如您所见,数组中有一些start_time和finish_time,我想使用带有分解器的分解函数来划分元素:",但分解除法值为 start_time 和完成时间,是否可以使用相同的分解器分解值

这是您正在寻找内容的原始草图:

<?php
$input = array (
  0 =>  'class_name:10:date:2014-03-08:start_time:10:15:finish_time:12:15:subject:Maths:exam_details:Extra comments:',
  1 =>  'class_name:10:date:2014-03-07:start_time:10:15:finish_time:12:15:subject:Eng:exam_details:Extra comments:',
  2 =>  'class_name:10:date:2014-03-06:start_time:10:15:finish_time:12:15:subject:Science:exam_details:Extra comments:',
  3 =>  'class_name:10:date:2014-03-06:start_time:10:15:finish_time:12:15:subject:Science:exam_details:Extra comments:',
  4 =>  'class_name:10:date:2014-03-06:start_time:10:15:finish_time:12:15:subject:Science:exam_details:Extra comments:',
  5 =>  'class_name:10:date:2014-03-06:start_time:10:15:finish_time:12:15:subject:Science:exam_details:Extra comments:'
);
$tokens = array();
foreach ($input as $key=>$line) {
  echo sprintf("line %s:n", $key);
  preg_match('/class_name:([0-9]+):date:([0-9]{4}-[0-9]{2}-[0-9]{2}):start_time:([0-9]{2}:[0-9]{2}):finish_time:([0-9]{2}:[0-9]{2}):subject:([^:]+)/',
             $line , $tokens);
  var_dump($tokens);
  echo "nn";
};
?>

可能regular expression必须根据数组的实际内容进行调整。它目前没有捕获每行末尾的标记,因为语法在您的问题中不清楚。但是你应该得到基本的想法。

它的输出是(缩短):

line 0:
array(6) {
  [0] =>
  string(78) "class_name:10:date:2014-03 08:start_time:10:15:finish_time:12:15:subject:Maths"
  [1] =>
  string(2) "10"
  [2] =>
  string(10) "2014-03-08"
  [3] =>
  string(5) "10:15"
  [4] =>
  string(5) "12:15"
  [5] =>
  string(5) "Maths"
}

line 1:
array(6) {
  [0] =>
  string(76) "class_name:10:date:2014-03-07:start_time:10:15:finish_time:12:15:subject:Eng"
  [1] =>
  string(2) "10"
  [2] =>
  string(10) "2014-03-07"
  [3] =>
  string(5) "10:15"
  [4] =>
  string(5) "12:15"
  [5] =>
  string(3) "Eng"
}

[...]

输出目前呈现为 php 的 cli 使用,这使得开发这些东西更容易。如果要在浏览器中对其进行测试,则必须将n换行符替换为<br>标记等。

在你的情况下没有神奇的方法。

你必须自己。

这是你能做什么的想法

$keys = array('class_name','date','start_time','finish_time','subject','exam_details');
// for each line of your table
$parseData = retreiveData($keys, $stringData);
function retreiveData($keys, $stringData)
{   
    $results = array();
    $returnValue = preg_split('/('.implode('|', $keys).')/', $stringData, -1);
    for ($i =0; $i<count($results); $i++) {
        $results[$i] = rtrim(ltrim($returnValue[$i],':'),':');
    }
    return $results;
}

实际数组:

$data =Array
(
    0 =>  'class_name:10:date:2014-03-08:start_time:10:15:finish_time:12:15:subject:Maths:exam_details:Extra comments:',
    1 =>  'class_name:10:date:2014-03-07:start_time:10:15:finish_time:12:15:subject:Eng:exam_details:Extra comments:',
    2 =>  'class_name:10:date:2014-03-06:start_time:10:15:finish_time:12:15:subject:Science:exam_details:Extra comments:',
    3 =>  'class_name:10:date:2014-03-06:start_time:10:15:finish_time:12:15:subject:Science:exam_details:Extra comments:',
    4 =>  'class_name:10:date:2014-03-06:start_time:10:15:finish_time:12:15:subject:Science:exam_details:Extra comments:',
    5 =>  'class_name:10:date:2014-03-06:start_time:10:15:finish_time:12:15:subject:Science:exam_details:Extra comments:'
);

用于每个循环迭代数组

foreach($data as $dt=>$val){
use explode function and create an array
$datas= explode(':',$val);

根据您的需要创建一个数组

$new_array[] = array($datas[0],$datas[1],$datas[2].':'.$datas[3],$datas[4].':'.$datas[5].':'.$datas[6],$datas[7].':'.$datas[8].':'.$datas[9],$datas[10],$datas[11],$datas[12],$datas[13]);

echo "<pre>";
print_r($new_array);

会给你想要的结果

相关内容

  • 没有找到相关文章