Jump to content

Recommended Posts

I have a string [block of text from a file via fopen()] that I would like to split into a multi-dimensional array.  Here the contents of the file that is opened:

 

Father {

    son

    child {

        bob

        john

        jim

    }

}

mother {

    daughter

    kin

}

bigfoot {

    hairy

    kisses

}

 

How can I use preg_split() to result in the following array or if there is a better way how can this be done:

 

Array

(

    [ 0 ] => Father

        (

            [ 0 ] => son

            [ 1 ] => child

                (

                    [ 0 ] => bob

                    [ 1 ] => john

                    [ 2 ] => jim

                )

        )

 

    [ 1 ] => mother

        (

            [ 0 ] => daughter

            [ 1 ] => kin

        )

 

    [ 2 ] => bigfoot

        (

            [ 0 ] => hairy

            [ 1 ] => kisses

        )

 

)

 

Thanks for your help.

 

roydukkey

 

Link to comment
https://forums.phpfreaks.com/topic/121614-problems-with-regular-expressions/
Share on other sites

While I'm not sure about the multidimensional array part, instead of using fopen, you can use file(filepath and file name here). This opens a file and extracts its contents into an array. I'm guessing that depending on what your file is structured like to begin with would determine the array that file() creates..

 

The from there, you can list the array via foreach(), then if you feel the need to break if down further, perhaps use preg_split (not entirely sure if this last bit is feasable).

 

You can read up on file() here:

http://us2.php.net/manual/en/function.file.php

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.