...

View Full Version : Resolved .ini.php - multidimensional associative = possible?



cfructose
04-01-2010, 12:04 PM
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:


[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.

Fou-Lu
04-01-2010, 03:47 PM
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:


[form]
name[] = Name
name[] = TRUE
;etc
notify[] = "Send follow-up comments via email"
notify[] = FALSE
;etc


and when run with


print_r(parse_ini_file('file.ini', true));

Should be:


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

cfructose
04-01-2010, 06:38 PM
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:


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?

Fou-Lu
04-01-2010, 06:57 PM
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:


label="Subscribe to the COMPANY_NAME newsletter"




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.

cfructose
04-01-2010, 11:28 PM
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!).

MattF
04-01-2010, 11:40 PM
Why not just use an ordinary file containing arrays for your config and include the file?

cfructose
04-02-2010, 12:35 AM
@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..."

:D

MattF
04-02-2010, 05:55 PM
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..."

:D

Frequently, :D 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. :D

cfructose
04-03-2010, 09:01 AM
Touché!

sangoku
04-07-2010, 04:03 PM
Frequently, :D 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. :D

just registerd to say nice chat :) and to ask CVS????

cfructose
04-08-2010, 08:18 PM
@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...

sangoku
04-09-2010, 12:43 AM
@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[] =" :rolleyes::eek: was my reaction i just wanted to make some ini file from some copy paste list....

cfructose
04-09-2010, 01:23 AM
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! :D It's definitely my favourite American phrase.)

sangoku
04-09-2010, 08:06 AM
Yowsers!

(Now, where did I first hear that phrase...? Oh yeah, in Scooby Doo! :D 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 :thumbsup: 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[][] =

sunwukung
07-15-2010, 04:20 PM
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-file-into-a-multi-dimensional-array

which is essentially a stripped down version of Zend_Config_Ini



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum