有没有一个简单的解决方案,我可以把一个函数内部解决以下问题:
我有多个数组,如下所示:
Array
(
[0] => A
[1] => 100_1
[2] => 1
[3] => 1184
[4] => 0
)
Array
(
[0] => A
[1] => 100_2
[2] => 2
[3] => 1185
[4] => 0
)
Array
(
[0] => A
[1] => 100_3
[2] => 3
[3] => 1186
[4] => 0
)
Array
(
[0] => A
[1] => 101_2
[2] => 2
[3] => 1188
[4] => 0
)
Array
(
[0] => A
[1] => 302
[2] => 0
[3] => 1161
[4] => 0
)我想要做的是选择所有只有_1,2,3的值,
并删除_1,2,3,所需的结果将是:
Array
(
[0] => A
[1] => 100
[2] => 1
[3] => 1184
[4] => 0
)
Array
(
[0] => A
[1] => 100
[2] => 2
[3] => 1185
[4] => 0
)
Array
(
[0] => A
[1] => 100
[2] => 3
[3] => 1186
[4] => 0
)
Array
(
[0] => A
[1] => 101
[2] => 2
[3] => 1188
[4] => 0
)
Array
(
[0] => A
[1] => 302
[2] => 0
[3] => 1161
[4] => 0
)注意只有包含_1,2,3的值已被更改。
非常感谢您的帮助,非常感谢。
我到目前为止的代码是:
foreach($data as $line) {
if(substr($line,0,1)=="A") {
if(!$first) {
$parts = explode(chr(9), $line);
list($num1, $num2) = explode('_', $parts[1]); //this code needs to come first
$parts[2] = isset($num2) ? $num2 : $parts[2]; //it replaces key[2] with _* (1,2,3)
//echo "
"; print_r($parts); echo "";
//the code to remove _* needs to go here.
$pos = strpos($parts[1], '_'); // this was all it needed a simple two liner
if($pos !== false) $parts[1] = substr($parts[1], 0, $pos);