mohsin
mohsin

Reputation: 123

Array keep on repeating in foreach

I have an array holding string in it's values, I am finding underscore in each value breaking it and inserting it into new array, It's doing all right but only for first array in second array it repeats first array also.

For Example

$someArraVal[] = 'abc_xyz__vr1_vr2';
$someArraVal[] = 'emf_ccc__vr2_vr3';

First I am getting everything after double underscore then exploding them with underscore and trying to have array like below

Array
(
[0] => Array
    (
        [0] => vr1
        [1] => vr2
    )

[1] => Array
    (
        [2] => vr3
        [3] => vr4
    )
)

but I am getting

Array
(
[0] => Array
    (
        [0] => vr1
        [1] => vr2
    )

[1] => Array
    (
        [0] => vr1
        [1] => vr2
        [2] => vr3
        [3] => vr4
    )
)

CODE

    $someArraVal[] = 'abc_xyz__vr1_vr2';
    $someArraVal[] = 'emf_ccc__vr3_vr4';

    $arr1 = [];
    $arr2 = [];
    $xmx = [];


    foreach ($someArraVal as $key => $value) {

        $afterunderscore = substr($value, strpos($value, "__") + 1);
        // $addPipe = str_replace("_","|",$afterunderscore);
        $abc = substr($afterunderscore, 1);
        $arr1 = explode('_',$abc);

        foreach ($arr1 as $k => $v) {
            $arr2[] = $v;
        }

        $xmx[] = $arr2;

    }

    printR($xmx);

Upvotes: 2

Views: 1157

Answers (4)

mickmackusa
mickmackusa

Reputation: 48011

Another technique using a single iterated function call...

Code: (Demo)

$someArraVal[] = 'abc_xyz__vr1_vr2';
$someArraVal[] = 'emf_ccc__vr2_vr3';

foreach ($someArraVal as $value) {
    $xmx[] = preg_split('~(?:[^_]+_)*_~', $value, 0, PREG_SPLIT_NO_EMPTY);
}

print_r($xmx);

Output:

Array
(
    [0] => Array
        (
            [0] => vr1
            [1] => vr2
        )

    [1] => Array
        (
            [0] => vr2
            [1] => vr3
        )

)

The pattern consumes the undesired substrings and treats them as delimiters. This leaves you with only the substrings that you want.


If the idea of regex scares you, here's a non-regex solution with just two calls per iteration:

foreach ($someArraVal as $value) {
    $xmx[] = array_slice(explode('_', $value, 5), -2);
}
// same result array

Upvotes: 0

Nidhi Gupta
Nidhi Gupta

Reputation: 41

Second array keeps repeating because you are looping inside the array which will take all the values.

<?php

$someArraVal[] = 'abc_xyz__vr1_vr2';
    $someArraVal[] = 'emf_ccc__vr3_vr4';

    $arr1 = [];
    foreach ($someArraVal as $key => $value) {

        $afterunderscore = substr($value, strpos($value, "__") + 1);
        $abc = substr($afterunderscore, 1);
        $arr1[] = explode('_',$abc);    
    }

echo '<pre>';
print_r($arr1);
echo '</pre>';

Please check : https://prnt.sc/oitez2

Upvotes: 1

dWinder
dWinder

Reputation: 11642

You never reset $arr2 so you append more data each iteration.

You can add $arr2 = [] right after the foreach.

Better solution will be to do:

foreach ($someArraVal as $key => $value) {
    $afterunderscore = substr($value, strpos($value, "__") + 1);
    $xmx[] = explode('_',substr($afterunderscore, 1));
}

Live example: 3v4l

Upvotes: 1

ascsoftw
ascsoftw

Reputation: 3476

You need to empty the array $arr2 at the start of foreach loop.

.
.
.


    foreach ($someArraVal as $key => $value) {

        $arr2 = []; //Empty the $arr2 at begining of the loop
        .
        .
        .

    }


Upvotes: 2

Related Questions