php数组在多个数组中选择一个键值并更改它们

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);

Assuming the _1 etc. always happens in array[1]

foreach($array as $row) {
    $pos = strpos($row[1], '_');
    if($pos !== false) $row[1] = substr($row[1], 0, $pos);
}

you can use the following:

function formatArray($array)
{
    if(is_array($array))
    {
        foreach($array as $key=>$value)
        {
            $array[$key] = str_replace('_1', '', $value);
            $array[$key] = str_replace('_2', '', $value);
            $array[$key] = str_replace('_3', '', $value);
        }
        return $array;
    }
    return false;

}

Call this function with every array as parameter or you can assign the complete array to and parent array and then add one more foreach to loop through it.

Just write a function which uses preg_replace to alter your data:

<?php
$test_data = array("A", "100_1", "0", "1184", "0");

function removeValuesWithUnderScore(array $arr){
    //Loop over array
    foreach($arr as $key => $value){
        $arr[$key] = preg_replace('/_[1-3]/', '', $value);
    }

    //Return altered array
    return $arr;
}

$test_data = removeValuesWithUnderScore($test_data);

Try this :

$array = array("A", "100_1", 0, 1184, 0);
$array = array_map(
    function($str) {
        return preg_replace('/\_[\d]+/', '', $str);
    },
    $array
);

print_r($array)

Edit : If OP needs only _1, _2 and _3 to be removed :

$array = array("A", "100_3", 0, 1184, 0);
$array = array_map(
    function($str) {
        return preg_replace('/\_[1|2|3]/', '', $str);
    },
    $array
);

print_r($array)

presuming you want any value with _xxx appended to be replaced:

foreach($array as &$element){
    $element = strstr($element, '_', true)?:$element;
}