$arr = ['Governance->Policies->Prescriptions->CAS Alerts',
'Users->User Departments->Department Hierarchy',
'Settings->Registrar->Finance',
'Logs->Second Opinion Log'];
This is array and I want to convert it into string like below
The string should be one it just concate in one string.
Governance->Policies
Governance->Prescriptions
Governance->CAS Alerts
Users->User Departments
Users->Department Hierarchy
Settings->Registrar
Settings->Finance
Logs->Second Opinion Log
$arr = ['Governance->Policies->Prescriptions->CAS Alerts',
'Users->User Departments->Department Hierarchy',
'Settings->Registrar->Finance',
'Logs->Second Opinion Log'];
$temp = '';
for($i = 0; $i < count($arr); $i++){
$arrVal = [];
$arrVal = explode('->',$arr[$i]);
if(count($arrVal) > 1){
for($j=0; $j < count($arrVal); $j++){
if($j == 0){
$temp .= $arrVal[$j];
}else{
$temp .='->'.$arrVal[$j]."n";
if($j == count($arrVal) - 1){
$temp .= "n";
}else{
$temp .= substr($temp, 0, strpos($temp, "->"));
}
}
}
}
}
echo $temp;
3
Answers
Here the solution:
Output :
Demo : https://onlinephp.io/c/9eb2d
Or, using JOIN()
Demo : https://onlinephp.io/c/c1826
As you iterate your array of arrow-delimited strings, explode each element on its arrows, separate the the first value from the rest, then iterate the remaining elements from the explosion and prepend the cached, first value and push into the result array.
Code: (Demo)