OK I have a text file that looks like this

....................
....................
19511425 ./public_html/download
....................
....................
12665106 ./public_html/scripts
....................

Right now I have a regex that returns an array of the directory sizes but not in the format that I wanted.

Here is the REGEX:


preg_match_all('/(\d+)\s+(.\/public_html\/(download|scripts))\n/si',$ducache, $matches);

print_r($matches);

It returns:

Array
(
    [0] => Array
        (
            [0] => 5694979	./public_html/download

        [1] => 19511425	./public_html/scripts

    )

[1] => Array
    (
        [0] => 5694979
        [1] => 19511425
    )

[2] => Array
    (
        [0] => ./public_html/download
        [1] => ./public_html/scripts
    )

[3] => Array
    (
        [0] => download
        [1] => scripts
    )

)

I am trying to group the size and directories in one array, something like this:

Array
(
    [0] => Array
        (
            [0] => 5694979

        [1] => ./public_html/download

    )

[1] => Array
    (
        [0] => 19511425

        [1] => ./public_html/scripts

    )

)

Can anybody tell me how to do it?

Thanks in advance!

    $newArray = array();
    foreach($matches[1] as $key => $value)
    {
       $newArray[] = array($value, $matches[2][$key]);
    }
    

      NogDog, I am aware of that solution.
      But being a PHP geek, I am trying to see if there is a more elegant solution, such as tweaking the REGEX.

        The flag PREG_SET_ORDER may get you closer at least, it would output something like

        Array
        (
            [0] => Array
                (
                    [0] => 19511425 ./public_html/download
        
                [1] => 19511425
                [2] => ./public_html/download
                [3] => download
            )
        
        [1] => Array
            (
                [0] => 12665106 ./public_html/scripts
        
                [1] => 12665106
                [2] => ./public_html/scripts
                [3] => scripts
            )
        
        )
        

        Getting rid of [2] and [3] would be easy by omitting the parentheses or marking them as not to be captured, like

        preg_match_all('#(\d+)\s+./public_html/(?:download|scripts)\n#si',$ducache, $matches, PREG_SET_ORDER); 
        

        But I don't think it is possible to avoid the complete match [0] being captured.

          Thanks xblue, thats exactly what Im looking for !

          xblue wrote:

          The flag PREG_SET_ORDER may get you closer at least, it would output something like

          Array
          (
              [0] => Array
                  (
                      [0] => 19511425 ./public_html/download
          
                  [1] => 19511425
                  [2] => ./public_html/download
                  [3] => download
              )
          
          [1] => Array
              (
                  [0] => 12665106 ./public_html/scripts
          
                  [1] => 12665106
                  [2] => ./public_html/scripts
                  [3] => scripts
              )
          
          )
          

          Getting rid of [2] and [3] would be easy by omitting the parentheses or marking them as not to be captured, like

          preg_match_all('#(\d+)\s+./public_html/(?:download|scripts)\n#si',$ducache, $matches, PREG_SET_ORDER); 
          

          But I don't think it is possible to avoid the complete match [0] being captured.

            Write a Reply...