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

时间:2014-04-08 07:56:13

标签: php arrays multidimensional-array foreach array-merge

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的分频值和完成时间。是否可以使用相同的分隔符来爆炸值

3 个答案:

答案 0 :(得分:2)

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

<?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 "\n\n";
};

?>

可能必须根据数组的实际内容调整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"
}


[...]

当前为cli使用php渲染输出,这使得开发这类事物变得更容易。如果您想在浏览器中对其进行测试,则必须使用\n标记等替换<br>换行符。

答案 1 :(得分:0)

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

你必须自己动手。

这是你能做什么的想法

$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;
}

答案 2 :(得分:0)

实际数组:

$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);

会给你想要的结果