skip to Main Content
$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


  1. 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
    
    Login or Signup to reply.
  2. $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

    Login or Signup to reply.
  3. 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)

    $result = [];
    foreach ($arr as $string) {
        $parts = explode('->', $string);
        $parent = array_shift($parts);
        foreach ($parts as $child) {
            $result[] = "{$parent}->$child";
        }
    }
    var_export($result);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search