douping6871 2018-06-10 04:48
浏览 74
已采纳

将String转换为数组或多维数组,并将特定字符串作为键

I want to convert string into multidimensional array, so that I can display it in unordered list

Contents of $notes variable:

Conditions:
  Adult fares apply from 13 years old and above. Child fares apply from 4 to 
  12 years old
  Infants not included

What to bring:
  Sunscreen, water bottle, hat, sunglasses, camera, small 7kg overnight bag 
  if using the hop on hop off option

What to wear:
  Warm clothes, windbreaker, comfortable walking shoes

Insurance: 
  We highly recommend all passengers have travel insurance coverage

Optional extras paid on arrival:
  Helicopter joy flight over the 12 Apostles AUD145

Not included:
  Dinner (at own expen

What want an output like this

  [
     'conditions => ['some','some'],
     'what to bring' => ['content','content']
     .......
  ]

What I tried so far is using explode function

   explode(',',$notes)

But this is the output which I donot want

 array:8 [
    0 => """
       Conditions:

       Adults fares ....
       

      What to bring
      .....
    1 => "water bottle"
    2 => "hat"
    ......
 ]

So expected output as unordered list

     conditions
          1. ....
          2.....
     what to bring
           1....
           2....
     Inurance
           1..
           2...

notice that keys(conditions, what to bring..) are dynamic names, so it may change from time to time but the format is the the same

Any suggestions?

  • 写回答

1条回答 默认 最新

  • douzhang7603 2018-06-10 05:58
    关注

    You can explode the string by EOL, loop thru the array and check if the string ends with :, if it does, use it as the key.

    $notes = ''; //Your string here
    
    //Init variables
    $final = array();
    $tempKey = "";
    
    //Convert the string into an array
    $arr = array_filter(explode(PHP_EOL, $notes), 'trim');
    
    //Loop thru the array
    foreach($arr as $val) {
        if ( substr(trim($val), -1) === ':' ) $tempKey = rtrim(trim($val),":");
        else $final[ $tempKey ][] = trim($val);
    }
    
    echo "<pre>";
    print_r( $final );
    echo "</pre>";
    

    This will result to:

    Array
    (
        [Conditions] => Array
            (
                [0] => Adult fares apply from 13 years old and above. Child fares apply from 4 to
                [1] => 12 years old
                [2] => Infants not included
            )
    
        [What to bring] => Array
            (
                [0] => Sunscreen, water bottle, hat, sunglasses, camera, small 7kg overnight bag
                [1] => if using the hop on hop off option
            )
    
        [What to wear] => Array
            (
                [0] => Warm clothes, windbreaker, comfortable walking shoes
            )
    
        [Insurance] => Array
            (
                [0] => We highly recommend all passengers have travel insurance coverage
            )
    
        [Optional extras paid on arrival] => Array
            (
                [0] => Helicopter joy flight over the 12 Apostles AUD145
            )
    
        [Not included] => Array
            (
                [0] => Dinner (at own expen
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化