Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Regular Coder
    Join Date
    Feb 2007
    Location
    London
    Posts
    225
    Thanks
    16
    Thanked 2 Times in 2 Posts

    .ini.php - multidimensional associative = possible?

    I don't suppose there's any syntax that will allow a further dimension with an associative array in a .ini.php file? I realise that I can have numerically indexed sub-arrays, but I was hoping for something of the form:

    PHP Code:
    [form]
    name[label] = Name
    name
    [required] = TRUE
    ;etc
    notify
    [label] = "Send follow-up comments via email"
    notify[checked] = FALSE
    ;etc 
    Naturally, parse_ini_file throws errors when trying to parse this.

    So, what I'm looking for (and I don't hold out much hope!) is effectively something that will process sections within sections.
    Last edited by cfructose; 04-02-2010 at 01:24 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Sorry I don't believe that there is. What you can do is change logic flow if you wanted in order to numerically group together the types instead of the names (so you end up with 'label' => array(0 => Name, 1 => "Send follow-up comments via email")) and so forth.
    You can of course parse you're sections using parse_ini_file('file.ini', true), but I don't believe that multiple depths are allowed in PHP.

    So I believe you'll be stuck with:
    Code:
    [form] 
    name[] = Name 
    name[] = TRUE 
    ;etc 
    notify[] = "Send follow-up comments via email" 
    notify[] = FALSE 
    ;etc
    and when run with
    PHP Code:
    print_r(parse_ini_file('file.ini'true)); 
    Should be:
    Code:
    Array
    (
        [form] => Array
            (
                [name] => Array
                    (
                        [0] => 'Name' // Or whatever the constant Name has been defined as
                        [1] => true
                    )
                [notify] => Array
                    (
                        [0] => 'Send follow-up comments via email'
                        [1] => false
                    )
            )
    )
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder
    Join Date
    Feb 2007
    Location
    London
    Posts
    225
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Thanks for that.

    Frustratingly, this is gonna require shuffling about God knows how many foreach loops in God knows how many scripts in order to cater for this new config array structure!

    But at least with numerical indexing it's still possible to achieve all this.

    Using .ini.php files turns out to create various new obstacles...
    Something else just occurred to me (and I presume I'm stuck with this again, but perhaps you could confirm...?):
    I can no longer concatenate strings and constants in values.

    So:

    Code:
    label = "Subscribe to the ".COMPANY_NAME." newsletter"
    is no longer possible.

    I know that a value which is nothing but a constant will be incorporated into the array when parsed, but no concatenation is possible, right?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That I can't confirm ATM I'm afraid. What you can try is to go without the concat options. Parse_ini_file may work like the C preprocessor, so you may be able to just try this:
    Code:
    label="Subscribe to the COMPANY_NAME newsletter"
    PHP Code:
    define('COMPANY_NAME''Company Name');
    print_r(parse_ini_file('file.ini'true)); 
    Try that, will that work? If not, then replacements could be done through alternative means such as replacements. Ini files may not be preferable for what you're trying if you're dealing with a lot of dynamic data; ini files are more designed to handle unchanged configuration data.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    cfructose (04-02-2010)

  • #5
    Regular Coder
    Join Date
    Feb 2007
    Location
    London
    Posts
    225
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Alas, it doesn't work like the C preprocessor. It just prints the constant name as opposed to the value.

    Well, you got me thinking.. perhaps ini files aren't the right tool for this job.

    The values in this ini aren't dynamic at all - the only thing being generated dynamically was that one constant, COMPANY_NAME (which was needed in lots of other classes, so I wanted to have it propagate throughout all scripts upon editing it in just one place).

    But even though these config vars are static, I'm having to jump through all sorts of hoops to make this work.

    Thanks for all you help (again!).

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Why not just use an ordinary file containing arrays for your config and include the file?

  • #7
    Regular Coder
    Join Date
    Feb 2007
    Location
    London
    Posts
    225
    Thanks
    16
    Thanked 2 Times in 2 Posts
    @MattF

    Hehe! That would be easy, wouldn't it!
    You're right, of course. An ordinary file is what I used to have in this context, and it worked fine.

    But I got excited a few days ago when I discovered 'parse_ini_file' for the first time, and wanted to experiment with using it in my class __constructs.
    I was simply seduced by the thought of not having to explicitly send config variables to a class manually as arguments.

    D'ya ever think anything like the following...?

    "Ooh, this is cool! I know, I think I'll set aside the more important jobs that I'm supposed to be doing, and spend all day rewriting some code that was just fine to begin with... Hmm, this is getting a tad knotty... well, at least I'll have familiarised myself with a new technique... Hang on, this is turning out to be inferior to what I had originally... Oh Hell, I've refactored everything now (and didn't keep a copy of the original code), so I might as well plough ahead... I'll find a way to make this work, even if it kills me... Well, this is all strangely enjoyable. I think I need my head examined..."


  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by cfructose View Post
    D'ya ever think anything like the following...?

    "Ooh, this is cool! I know, I think I'll set aside the more important jobs that I'm supposed to be doing, and spend all day rewriting some code that was just fine to begin with... Hmm, this is getting a tad knotty... well, at least I'll have familiarised myself with a new technique... Hang on, this is turning out to be inferior to what I had originally... Oh Hell, I've refactored everything now (and didn't keep a copy of the original code), so I might as well plough ahead... I'll find a way to make this work, even if it kills me... Well, this is all strangely enjoyable. I think I need my head examined..."

    Frequently, but I always make sure I backup working code frequently. Make a few notable code changes, copy over and start working in the new directory. Makes sure you always have a usable version before something got screwed.

  • #9
    Regular Coder
    Join Date
    Feb 2007
    Location
    London
    Posts
    225
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Touché!

  • #10
    New Coder
    Join Date
    Apr 2010
    Posts
    20
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by MattF View Post
    Frequently, but I always make sure I backup working code frequently. Make a few notable code changes, copy over and start working in the new directory. Makes sure you always have a usable version before something got screwed.
    just registerd to say nice chat and to ask CVS????

  • #11
    Regular Coder
    Join Date
    Feb 2007
    Location
    London
    Posts
    225
    Thanks
    16
    Thanked 2 Times in 2 Posts
    @sangoku

    just registerd to say nice chat and to ask CVS????
    Welcome to the forum!

    For a moment I thought that was a typo for Comma Separated Values, then I googled it and learned... (yep, it was a new one on me!)... that it stands for "Concurrent Versions System".

    A think a second "touché" is in order!

    My poor-man's CVS is simply the multiple versions that my online backup software saves for me. It's rescued me from disaster often enough, but perhaps it's time I investigated using a true CVS. After all, this was one occasion where 3 historical copies of the script in question didn't go back far enough to save me from the overwritten data!

    ***

    P.S. In case anyone's interested, I've persevered with the .ini files, and after converting a few foreach loops to for loops to cater for the inevitable numerically-indexed arrays within the .ini's sections, and after finding ways around using constants, everything's working a treat! I feel a song coming on...

  • #12
    New Coder
    Join Date
    Apr 2010
    Posts
    20
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by cfructose View Post
    @sangoku



    Welcome to the forum!

    For a moment I thought that was a typo for Comma Separated Values, then I googled it and learned... (yep, it was a new one on me!)... that it stands for "Concurrent Versions System".

    A think a second "touché" is in order!

    My poor-man's CVS is simply the multiple versions that my online backup software saves for me. It's rescued me from disaster often enough, but perhaps it's time I investigated using a true CVS. After all, this was one occasion where 3 historical copies of the script in question didn't go back far enough to save me from the overwritten data!

    ***

    P.S. In case anyone's interested, I've persevered with the .ini files, and after converting a few foreach loops to for loops to cater for the inevitable numerically-indexed arrays within the .ini's sections, and after finding ways around using constants, everything's working a treat! I feel a song coming on...
    Thank you the forum is a good one good and professional ppl are here which i am very Very glad! XD i was on the search how to install a local CVS... i dont need online development i just need saving from my clumsiness....

    today i accidentally replaced all "." signs in my project, which is over 8k lines of code, with "image[] =" was my reaction i just wanted to make some ini file from some copy paste list....

  • #13
    Regular Coder
    Join Date
    Feb 2007
    Location
    London
    Posts
    225
    Thanks
    16
    Thanked 2 Times in 2 Posts
    today i accidentally replaced all "." signs in my project, which is over 8k lines of cod
    Yowsers!

    (Now, where did I first hear that phrase...? Oh yeah, in Scooby Doo! It's definitely my favourite American phrase.)

  • #14
    New Coder
    Join Date
    Apr 2010
    Posts
    20
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by cfructose View Post
    Yowsers!

    (Now, where did I first hear that phrase...? Oh yeah, in Scooby Doo! It's definitely my favourite American phrase.)
    ye i was lucky i tested the rar generator class day before on my root folder so i had a fresh backup... now i dont go coding without it if you use windows i recommend you TortoiseCVS-1.10.10.exe i am using it on windows 7 bit without problems.... it is a bit tricky untill you get it how it works but then it is pretty natural to use
    edit

    to "stay" on the topic of this post... XD
    it is possible to create multidimensional arrays in a ini file... it just is not so easy to keep track what is what...

    you can do

    name[][] =
    Last edited by sangoku; 04-09-2010 at 09:31 AM.

  • #15
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    JFTR - I was looking for an answer to the same problem today, and discovered this little gem (if you still want to parse.ini files)

    http://codeaid.net/php/parse-ini-fil...ensional-array

    which is essentially a stripped down version of Zend_Config_Ini


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •