doufang6268 2013-09-26 17:05
浏览 21
已采纳

从字符串中的编号列表创建数组

I have a plain string like this:

1 //here is a number defines phrase name
09/25/2013 //here is a date
<i>some text goes here</i> //and goes text

2
09/24/2013 
text goes on and on

4 //as you can see, the numbers can skip another
09/23/2013
heya i'm a text

I need to create array from this but the numbers that defines phrases must give me the date of that line and return the text when i called it. Like,

$line[4][date] should give me "09/23/2013"

Is that possible, if possible can you explain me how to do this?

  • 写回答

3条回答 默认 最新

  • dongtuan1980 2013-09-26 17:16
    关注
    $stringExample = "1 
    09/25/2013 
    <i>some text goes here</i> 
    
    2
    09/24/2013 
    text goes on and on
    and on and on
    
    4 
    09/23/2013
    heya i'm a text";
    
    $data = explode("
    
    ", $stringExample);
    
    $line = array();
    
    foreach ($data as $block) {
    
        list($id, $date, $text) = explode("
    ", $block, 3);
    
        $index = (int) $id;
    
        $line[$index] = array();
    
        $line[$index]["date"] = trim($date);
    
        $line[$index]["text"] = trim($text);
    
    }
    

    var_dump($line) should output:

    array(3) {
      [1]=>
      array(2) {
        ["date"]=>
        string(10) "09/25/2013"
        ["text"]=>
        string(26) "<i>some text goes here</i>"
      }
      [2]=>
      array(2) {
        ["date"]=>
        string(10) "09/24/2013"
        ["text"]=>
        string(34) "text goes on and on
    and on and on"
      }
      [4]=>
      array(2) {
        ["date"]=>
        string(10) "09/23/2013"
        ["text"]=>
        string(15) "heya i'm a text"
      }
    }
    

    You can test it here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂