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

将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?

图片转代码服务由CSDN问答提供 功能建议

我想将字符串转换为多维数组,以便我可以在无序列表中显示它

$ notes变量的内容:

 条件:
成人票价适用于13岁及以上。 儿童票价从4岁到12岁不适用
婴儿不包括在内
 
带什么:
防晒霜,水瓶,帽子,太阳镜,相机,小型7公斤过夜袋
如果使用随上随下跳选项 
 
穿什么衣服:
暖和的衣服,风衣,舒适的步行鞋
 
保险:
我们强烈建议所有乘客都有旅行保险; 
 
可在抵达时支付额外费用:
直升机欢乐飞越12 使徒AUD145 
 
不包括:
晚餐(在自己的费用
   
 
 

什么想要这样的输出

 < 代码> [
'conditions =&gt; ['some','some'],
'带什么'=&gt; ['content','content'] 
 ....... 
  ] 
   
 
 

我到目前为止尝试的是使用爆炸功能

  explode(',',$ notes)  
   
 
 

但这是我不想要的输出

  array:8 [
 0 =&gt;  “
”“
条件:
 
成人票价...... 
 
 
携带什么
 ..... 
 1 =&gt;”wa  ter bottle“
 2 =&gt;  “hat”
 ...... 
] 
   
 
 

因此预期输出为无序列表

 条件
 1. .... 
 2 ..... 
带什么
 1 .... 
 2 .... 
 Inurance 
 1 .. 
 2 ..  。
   
 
 

请注意,键(条件,带来的内容......)是动态名称,因此它可能会不时更改,但格式相同< / p>

有任何建议吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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
            )
    
    )
    
    打赏 评论

相关推荐 更多相似问题