is there a simple solution I can put inside a function to resolve the following:
I have multiple arrays as follows:
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
)
What I'd like to do is to select all the values which have _1,2,3 only, and remove the _1,2,3 so the desired result would be:
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
)
Notice only values which contain _1,2,3 have been changed. Many thanks for your help it's much appreciated.
The code I have so far is:
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 "<pre>"; print_r($parts); echo "</pre>";
//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);