0

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);
2
  • How about you try first and come with a particular issue? Commented Aug 6, 2014 at 12:07
  • I have been all day - I made edits. I would say that 'Machavity' is the closest which I will test shortly. Commented Aug 6, 2014 at 13:14

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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)

7 Comments

Would not work as he said, only _1, _2 and _3 should be removed.
@kevdev : Can you explain that?
@kevdev : Ok I got it :)
"What I'd like to do is to select all the values which have _1,2,3 only, and remove the _1,2,3" the op tells us in his question he want to remove _1,2,3 (_1,_2,_3) only, if this is not a mistake (Could also be, but the text says otherwise) your code will also remove for example _4, _5, _6 and so on.
@kevdev : Ohh sorry got confused :)
|
0

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

3 Comments

Hello Machavity this is so dam close : $pos = strpos($parts[1], '_'); if($pos !== false) $parts[1] = substr($parts[1], 0, $pos - 1); But it removes a '0' so key [1] shows 10 but should show 100 etc
Its ok the last $pos - 1; needs to be $pos -0; now it works great :)
Machavity can you go to my profile and there's 1 unanswered question I think you can help?
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.