Home > Software engineering >  How To convert array in string like to concate string with first array word like array[0].'->
How To convert array in string like to concate string with first array word like array[0].'->

Time:10-28

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

CodePudding user response:

Here the solution:

<?php
//This might help full
$arr = ['Governance->Policies->Prescriptions->CAS Alerts',
        'Users->User Departments->Department Hierarchy',
        'Settings->Registrar->Finance',
        'Logs->Second Opinion Log'];
        
$result="";
$newline="<br>";
foreach($arr as $data){
    $wordlist=explode('->',$data);
    $firstword=$wordlist[0];
    $temp='';
    foreach($wordlist  as $key=>$value){
      if($key > 0){
        $temp.=$firstword."->".$value.$newline;
      }
    }
    $temp.=$newline;
    $result.=$temp;
}
echo $result;
?>

Output :

Governance->Policies
Governance->Prescriptions
Governance->CAS Alerts

Users->User Departments
Users->Department Hierarchy

Settings->Registrar
Settings->Finance

Logs->Second Opinion Log

CodePudding user response:

$arr = ['Governance->Policies->Prescriptions->CAS Alerts',
        'Users->User Departments->Department Hierarchy',
        'Settings->Registrar->Finance',
        'Logs->Second Opinion Log'];

foreach ($arr as $path) {
  $path_elements = explode('->', $path);
  if (count($path_elements) > 1) {
    $path_head = $path_elements[0];
    $path_tail = array_slice($path_elements, 1);
    foreach ($path_tail as $path_item) {
      echo $path_head, '->', $path_item, "<br>";
    }
    echo "<br>";  
  }
}

Demo : https://onlinephp.io/c/9eb2d

Or, using JOIN()

$arr = ['Governance->Policies->Prescriptions->CAS Alerts',
        'Users->User Departments->Department Hierarchy',
        'Settings->Registrar->Finance',
        'Logs->Second Opinion Log'];

foreach ($arr as $path) {
  $path_elements = explode('->', $path);
  if (count($path_elements) > 1) {
    $path_head = $path_elements[0];
    $path_tail = array_slice($path_elements, 1);
    echo
      $path_head,
      '->',
      join('<br>'. $path_head.'->',$path_tail),
      '<br><br>';
  }
}

Demo : https://onlinephp.io/c/c1826

  • Related