如何爆炸字符串数组并将结果存储在另一个数组中(PHP) [英] How to explode an array of strings and store results in another array (php)

查看:74
本文介绍了如何爆炸字符串数组并将结果存储在另一个数组中(PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有以下格式的文本文件:

 (400,530); 6.9; 5.7; 5.0; // ------>持续100个值。 

(500,530); 7.9; 5.1; 5.0;

(600,530); 6.7; 6.7; 7.2;

代码:

 <?php 
$ file = ./ Speed10.asc;
$ document = file_get_contents($ file);
$ rows = explode('(',$ document); //将文档分成行

foreach($ rows& $ rowvalue){
explode('; ',$ rowvalue);< -----如何将这些元素分别分配给数组的成员
??
}
}
?>

我试图创建2D数组,方法是先将其拆分为行,然后将元素拆分为';'

解决方案

示例输入:

  $ document ='(400,530); 6.9; 5.7; 5.0; ... 
(500,530); 7.9; 5.1; 5.0; ...
(600,530); 6.7 ; 6.7; 7.2; ...';

方法1(不包含分号的值存储在数组):

  foreach(explode( \r\n,$ document)as $ row){//通过返回分割内容,然后换行
$ result [] = explode(;,$ row); //用分号分割每行,然后用空格
}
var_export($ result );
/ *输出:
[
['(400,530)','6.9 ','5.7','5.0','...'],
['((500,530)','7.9','5.1','5.0','...'],
['(600,530)','6.7','6.7','7.2','...']
]
)* /

方法2(带有分号的值存储在数组中):

  foreach(explode( \r\n,$ document)as $ row){//通过返回然后换行
$ result [] = preg_split( '/(?<!,)/',$ row); //用逗号之前的空格分隔每一行
}
var_export($ result);
/ *输出:
[
['(400,530);','6.9;','5.7;','5.0;','...'],
['(500,530);','7.9;','5.1;','5.0;','...'],
['(600,530);',' 6.7;','6.7;','7.2;','...']
]
)* /

这是这两种方法的演示。 p>

请记住,我只关注循环中的字符串拆分。



根据您的环境,您可能需要通过删除 \r 来调整第一次爆炸。 code>或类似的货币。


Have text file in format:

(400, 530); 6.9; 5.7; 5.0;//------> continues for 100 values.

(500, 530); 7.9; 5.1; 5.0;

(600, 530); 6.7; 6.7; 7.2;

Code:

<?php
$file="./Speed10.asc";
$document=file_get_contents($file);
$rows = explode ('(', $document); //splits document into rows

foreach ($rows as &$rowvalue) {
     explode (';', $rowvalue);<----- How to assign each of these to member 
                                     of an array??
  }
}
?>

I'm trying to create 2D array, by splitting first into rows, then by element split by ';'

解决方案

Sample Input:

$document='(400, 530); 6.9; 5.7; 5.0; ...
(500, 530); 7.9; 5.1; 5.0; ...
(600, 530); 6.7; 6.7; 7.2; ...';

Method #1 (values without semi-colons stored in array):

foreach(explode("\r\n",$document) as $row){   // split the content by return then newline
    $result[]=explode("; ",$row);             // split each row by semi-colon then space
}
var_export($result);
/* Output:
    [
        ['(400, 530)','6.9','5.7','5.0','...'],
        ['(500, 530)','7.9','5.1','5.0','...'],
        ['(600, 530)','6.7','6.7','7.2','...']
    ]
) */

Method #2 (values with semi-colons stored in array):

foreach(explode("\r\n",$document) as $row){    // split the content by return then newline
    $result[]=preg_split('/(?<!,) /',$row);    // split each row by space not preceeded by comma
}
var_export($result);
/* Output:
    [
        ['(400, 530);','6.9;','5.7;','5.0;','...'],
        ['(500, 530);','7.9;','5.1;','5.0;','...'],
        ['(600, 530);','6.7;','6.7;','7.2;','...']
    ]
) */

Here is demo of both methods.

Keep in mind I am only focusing on the string splitting inside the loop. Kris' advice on file handling is advisable.

Depending on your environment, you may need to adjust the first explode by removing \r or similar.

这篇关于如何爆炸字符串数组并将结果存储在另一个数组中(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆