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