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
Share on other sites

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?

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites


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=

Link to comment
Share on other sites

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
                                        (
                                        )

                                )

                        )

                )

        )

)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.