Jump to content

Split String To Array


N-Bomb(Nerd)

Recommended Posts

Hello,

 

I have a string that contains a directory path much like: "/images/icons" and I want to take that string through a function and explode it into an array of directories that with add my directory structure array:

 

  • images
    • icons

 

Then if I were to send the string "/data/documents" to the function it would append to the existing array:

  • images
    • icons

    [*]data

    • documents

 

Here's the code that I've got thus far:

<?php

$global_structure = array();

generate_structure("./images/icons");
generate_structure("./data/documents");

function generate_structure($directory)
{
    if(!is_dir($directory))
        return false;

    $directory_structure = explode("/", $directory);

    // do something here to iterate over $global_structure and add new directories to equal to current directory path making the value an empty array.
}

?>

Link to comment
https://forums.phpfreaks.com/topic/255177-split-string-to-array/
Share on other sites

I forgot to mention that I want the generate_structure() function to be recursive so I can pass long directory paths and have it append to the array regardless of the depth. That way I'm able to pass in a directory path like "/images/icons/big/massive/giant/small/" and it would work.

So you are looking for nested arrays, in the following manner:

 

$global_structure[0] = array(array("images",array("icons")), "data",array("documents"))

 

If I have understood correctly, I think you may find this approach has a lot of overhead. Have you thought of creating each folder as an Object, where each Object has a parent?

This essentially does what you ask, but I think you need to think about whatever it is you are trying to do.

 

<?php
$GLOBALS['STRUCT'] = array();

function structure($array) {
    $folders = explode("/", $array);
    $current = &$GLOBALS['STRUCT'];
    
    foreach ($folders as $folder) {
        if (!empty($folder) && $folder != "." && $folder != "..") {
            $current[$folder] = array();
            $current = &$current[$folder];
        }
    }
}

$dir = "/poop/in/a/basket/";

structure($dir);

print_r($GLOBALS['STRUCT']);
?>

 

EDIT: Furthermore, you shouldn't be using globals here at all. Sorry I even posted it.

 

<?php
$structure = array();

function structure($array, $structure) {
    $folders = explode("/", $array);
    $current = &$structure;
    
    foreach ($folders as $folder) {
        if (!empty($folder) && $folder != "." && $folder != "..") {
            $current[$folder] = array();
            $current = &$current[$folder];
        }
    }
    
    return $structure;
}

$dir = "/poop/in/a/basket/";

$structure = structure($dir, $structure);

print_r($structure);
?>


function generate_structure($path)
{
   static $structure = array();
   static $index     = 0;
   
   $last = substr($path, strrpos($path, '/') + 1);
   $next = substr($path, 0, strrpos($path, '/'));
   if ( empty($last) )
      generate_structure($next);
   if ( !isset($structure[$index]) )
      $structure[$index] = array();
   $structure[$index][]  = $last;
   if ( empty($next) )
   {
      $structure[$index] = array_reverse($structure[$index]);
      $index++;
      return $structure;
   }
   generate_structure($next);
   return $structure;
}


generate_structure('/path/to/myfile/file.php');
generate_structure('/path/to/myfile/file.php');
generate_structure('/path/to/myfile/file.php');
$f = generate_structure('/path/to/myfile/file.php');
var_dump($f);

Output:

 

 

array
  0 => 
    array
      0 => string 'path' (length=4)
      1 => string 'to' (length=2)
      2 => string 'myfile' (length=6)
      3 => string 'file.php' (length=
  1 => 
    array
      0 => string 'path' (length=4)
      1 => string 'to' (length=2)
      2 => string 'myfile' (length=6)
      3 => string 'file.php' (length=
  2 => 
    array
      0 => string 'path' (length=4)
      1 => string 'to' (length=2)
      2 => string 'myfile' (length=6)
      3 => string 'file.php' (length=
  3 => 
    array
      0 => string 'path' (length=4)
      1 => string 'to' (length=2)
      2 => string 'myfile' (length=6)
      3 => string 'file.php' (length=

I take back what I said about rethinking this. This function is actually somewhat nice:

 

<?php
$structure = array();

function structure($dir, &$structure) {
    $folders = explode("/", $dir);
    $current = &$structure;
    
    foreach ($folders as $folder) {
        if (!empty($folder) && $folder != "." && $folder != "..") {
            if (!isset($current[$folder])) {
                $current[$folder] = array();
            }
            $current = &$current[$folder];
        }
    }
}

$dir = array();
$dir[] = "/poop/in/a/basket/";
$dir[] = "/poop/in/a/toilet/";
$dir[] = "/poop/bowl/";
$dir[] = "/random/directory";
$dir[] = "C:/Program Files";
$dir[] = "C:/Program Files/Windows Media Player";
$dir[] = "C:/Program Files/Microsoft Games";
$dir[] = "C:/Program Files/Microsoft Games/Chess";
$dir[] = "C:/Program Files/Microsoft Games/Chess/en-US";
$dir[] = "C:/Program Files/Microsoft Games/FreeCell";
$dir[] = "C:/Program Files/Microsoft Games/FreeCell/en-US";
$dir[] = "C:/Program Files/Microsoft Games/Hearts";
$dir[] = "C:/Program Files/Microsoft Games/Hearts/en-US";

foreach ($dir as $d) {
    structure($d, $structure);
}

echo "<pre>";
print_r($structure);
echo "</pre>";
?>

 

Output:

 

Array
(
    [poop] => Array
        (
            [in] => Array
                (
                    [a] => Array
                        (
                            [basket] => Array
                                (
                                )

                            [toilet] => Array
                                (
                                )

                        )

                )

            [bowl] => Array
                (
                )

        )

    [random] => Array
        (
            [directory] => Array
                (
                )

        )

    [C:] => Array
        (
            [Program Files] => Array
                (
                    [Windows Media Player] => Array
                        (
                        )

                    [Microsoft Games] => Array
                        (
                            [Chess] => Array
                                (
                                    [en-US] => Array
                                        (
                                        )

                                )

                            [FreeCell] => Array
                                (
                                    [en-US] => Array
                                        (
                                        )

                                )

                            [Hearts] => Array
                                (
                                    [en-US] => Array
                                        (
                                        )

                                )

                        )

                )

        )

)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.