I have an array which is a mix of strings that I want to keep and strings that I don't want to keep. Of the strings to keep, I need to truncate them from their first occurring slash.
My array:
$code = [
'PO/2022/0001', // modify
'abc', // remove
'xyz', // remove
'PO2022/0001', // remove
'XY/2022/0002', // modify
'PO/2022/0232' // modify
];
I am trying to explode the strings in the above array on / and if the newly generated array has 3 elements, then I need to push the first value into my result array.
Expected result from sample input:
['PO', 'XY', 'PO']
Can I know what is the better and efficient approach to do this?
This is what I have so far:
foreach ($code as $v) {
$nwCode = explode("/",$v);
if(count($nwCode) == 3) {
$nwAry[] = $newCode[0];
}
$nwCode = [];
}
echo '<pre>',print_r ($nwAry).'</pre>';