doumizhi0809 2017-10-25 02:09
浏览 165
已采纳

PHP使用字母作为索引从字母和数字字符串创建关联数组

I have strings of parameters which can vary in structure, and also vary in parameter order like this:

T01
T0101
T01C0.95
T01H3000C0.95(brackets indicate a comment
T01C0.95H3000(brackets indicate a comment

This needs to be made into an array like this:

T01H3000C0.95 or T01C0.95H3000
T01[H] = 3000
T01[C] = 0.95
ignore >(brackets indicate a comment

In the case that the T section is a length of 3 or 4 characters:

T0102H3000
"T"number1.number2[CP] = number3.number4
"T"number1.number2[H] = 3000

T will always be between 1 and 4 numbers long. Periods . and spaces should not split the string.

The first section is easy because I know it will always start with T. So I can work with the string like this:

foreach($lines as $line) {
    if (substr( $line, 0, 1 ) === "T"){
    $lineArray = [];
         $trimedLine ="";
    print_r($line." - Tool Found");
    print_r("<br>");

    $trimedLine = ltrim($line, 'T');
    $lineArray = preg_split("/[a-zA-Z]/",$trimedLine,2);
    print_r("T - ".$lineArray[0]);
    print_r("<br>");    
    print_r("strlen: ".strlen($lineArray[0]));
    print_r("<br>");

    if (strlen($lineArray[0])== 1 OR strlen($lineArray[0])== 2){
     ${"T".$lineArray[0]}= [];
    print_r(${"T".$lineArray[0]});
    print_r("<br>");     
    } else {
    if (strlen($lineArray[0])== 3 OR strlen($lineArray[0])== 4)
    {   
    ${"T".$lineArray[0]}= [];   
    ${"T".$lineArray[0]}["CP"] = substr($lineArray[0], 2); 
    print_r(${"T".$lineArray[0]}["CP"]);
    print_r("<br>");        
    } else {
    print_r("Strange string size");
    print_r("<br>");    
    }
    }
    }

Afterwards it becomes more complicated. There is a chance that the index will be more than a single letter ex. SH or SP.

I could slowly cut the string down part by part by using something similar to my test code but it seems extremely convoluted.

How should I finish breaking this into an array?

  • 写回答

1条回答 默认 最新

  • doujinyi1267 2017-10-25 03:03
    关注

    Using preg_replace() and parse_str(), Here is example to start with

    Demo

    <?php
    
    $input='T01H3000C0.95(brackets indicate a comment';
    
    /* from preg_replace above input we make 
       H=3000&C=0.95&, parse_str will put it in array
    */
    
    parse_str( 
          preg_replace("/([A-Za-z])([+-]?\d+(\.\d+)?)/",'$1=$2&',
                   preg_replace('/(^T01)|(\(.*)/','',$input) 
          ), 
          $array
    );
    
    print_r($array);
    
    
    ?>
    

    Output:

    $ php test.php 
    Array
    (
        [H] => 3000
        [C] => 0.95
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型