Home > Software design >  filtering change content values in all element array
filtering change content values in all element array

Time:11-28

i have value array like this

^ array:2 [▼
    0 => "random value1<tr>random string1</tr>random endvalue1"
    1 => "random value2<tr>random string2</tr>random endvalue2"
  )
]

how do i change the value of the dimensional array above to be like below

^ array:2 [▼
    0 => "random string1"
    1 => "random string2"
  )
]

im trying using

$array = array_map(function($key) {
    $exp = explode('<tr>', $tes);
    //$exp1 = explode('</tr>', exp[1]); -from this line3 my explode cant access array 
    return exp;

}, $myarray);
dd($array);

my array in line3 become like this

^ array:2 [▼
    0 => array:2 [▼
        0 => "random value1"
    1 => "random string1</tr>random endvalue1"
    ]
    1 => array:2 [▼
        0 => "random value2"
    1 => "random string2</tr>random endvalue2"
    ]
  )
]

how to improve conditions like this, or have another way that is better ?

CodePudding user response:

$data = [
    "random value1<tr>random string1</tr>random endvalue1",
    "random value2<tr>random string2</tr>random endvalue2",
];

$trim = collect($data)
    ->map(function (string $value) {
        preg_match('/(?<=<tr>)(.*)(?=<\/tr>)/', $value, $match);

        return empty($match) ? $value : $match[1];
    })
    ->toArray();

Could be done this way, using the framework collect method to map through the values & extract the string you want.

  • Related