Home > Mobile >  PHP - Generate Path URL from a string
PHP - Generate Path URL from a string

Time:10-28

I have a string like this : Indoformosa/Folder1/SubFolder1, and with those string will be generate an array for a breadcrumbs URL. The app needs an array format lke this:

[
    0 => [
        'label' => 'Indoformosa'
        'url' => 'Indoformosa'
    ]
    1 => [
        'label' => 'Folder1'
        'url' => 'Indoformosa/Folder1'
    ]
    2 => [
        'label' => 'SubFolder1'
        'url' => 'Indoformosa/Folder1/SubFolder1'
    ]
]

So far, my PHP code looked like this:

 $queryParamsPath = 'Indoformosa/Folder1/SubFolder1'
 $links = explode('/', $queryParamsPath);
 $links = array_map(function ($el) {
    return [
       'label' => $el,
       'url' => Url::to($el)
    ];
 }, $links);

The output looked like this:

[
    0 => [
        'label' => 'Indoformosa'
        'url' => 'Indoformosa'
    ]
    1 => [
        'label' => 'Folder1'
        'url' => 'Folder1'
    ]
    2 => [
        'label' => 'SubFolder1'
        'url' => 'SubFolder1'
    ]
]

CodePudding user response:

You were actually pretty close. I slightly change your code to use more standard language elements:

<?php

 $queryParamsPath = 'Indoformosa/Folder1/SubFolder1';

 $parts = explode('/', $queryParamsPath);
 $links = [];
 $path  = '';
 foreach ($parts as $part) {
     $links[] = [
       'label' => $part,
       'url'   => $path . $part,
    ];
    $path .= $part . '/';
 }
 
 print_r($links);

You might want to put this inside a function to isolate the variables from the global scope.

You can see the working code here: https://3v4l.org/Y6dSc

CodePudding user response:

An easy fix would introducing an extra array in which you wil 'hold' to url, and append the next part on each iteration:

<?php

 $queryParamsPath = 'Indoformosa/Folder1/SubFolder1';
 $links = explode('/', $queryParamsPath);
 $hold = [];
 $links = array_map(function ($el) use (&$hold) {
    $hold[] = $el;  
    return [
       'label' => $el,
       'url' => implode($hold, '/')
    ];
 }, $links);

Try it online!


A more dynamic fix would using array_reduce(), where you can use the carry to get the 'last' url, and append the current url to it, with a / between if it's not the first one:

<?php

    $queryParamsPath = 'Indoformosa/Folder1/SubFolder1';
    $links = explode('/', $queryParamsPath);

    $links = array_reduce($links, function($c, $i) {

        $existingUrls = $c[count($c) - 1]['url'] ?? '';
        $existingUrls .= ($existingUrls) ? ('/' . $i) : $i;

        $c[] = [
            'label' => $i,
            'url' => $existingUrls
        ];

        return $c;
    }, []);

Try it online!


Both provide the following output:

array(3) {
  [0]=>
  array(2) {
    ["label"]=>
    string(11) "Indoformosa"
    ["url"]=>
    string(11) "Indoformosa"
  }
  [1]=>
  array(2) {
    ["label"]=>
    string(7) "Folder1"
    ["url"]=>
    string(19) "Indoformosa/Folder1"
  }
  [2]=>
  array(2) {
    ["label"]=>
    string(10) "SubFolder1"
    ["url"]=>
    string(30) "Indoformosa/Folder1/SubFolder1"
  }
}
  •  Tags:  
  • php
  • Related