Reputation: 41
Working on some tutorials, I have seen PHP arrays are quite different from ColdFusion arrays, and that PHP does not have structures. I need to know what part of the following PHP code is possible in ColdFusion:
public $colors = array(
array(27,78,181), // blue
array(22,163,35), // green
array(214,36,7), // red
);
public $fonts = array(
'Antykwa' => array('spacing' => -3, 'minSize' => 27, 'maxSize' => 30, 'font' => 'AntykwaBold.ttf'),
'Candice' => array('spacing' =>-1.5,'minSize' => 28, 'maxSize' => 31, 'font' => 'Candice.ttf'),
'DingDong' => array('spacing' => -2, 'minSize' => 24, 'maxSize' => 30, 'font' => 'Ding-DongDaddyO.ttf'),
'Duality' => array('spacing' => -2, 'minSize' => 30, 'maxSize' => 38, 'font' => 'Duality.ttf'),
'Heineken' => array('spacing' => -2, 'minSize' => 24, 'maxSize' => 34, 'font' => 'Heineken.ttf'),
'Jura' => array('spacing' => -2, 'minSize' => 28, 'maxSize' => 32, 'font' => 'Jura.ttf'),
'StayPuft' => array('spacing' =>-1.5,'minSize' => 28, 'maxSize' => 32, 'font' => 'StayPuft.ttf'),
'Times' => array('spacing' => -2, 'minSize' => 28, 'maxSize' => 34, 'font' => 'TimesNewRomanBold.ttf'),
'VeraSans' => array('spacing' => -1, 'minSize' => 20, 'maxSize' => 28, 'font' => 'VeraSansBold.ttf'),
);
Another part of PHP is the foreach
loop like:
foreach($list as key=>$value) {
}
I think this could be done as a loop over a structure, but I am not sure.
Upvotes: 2
Views: 265
Reputation: 29870
The first example is just analogous to a CFML array, eg:
colors = [
[27,78,181], // blue
[22,163,35], // green
[214,36,7] // red
];
Whilst it's true that PHP doesn't have something called a "struct", it has an associative array, which is the same thing, for all intents and purposes. And your latter example is one of those. The CFML equiv (abbreviated) would be:
fonts = {
'Antykwa' = {'spacing' = -3, 'minSize' = 27, 'maxSize' = 30, 'font' = 'AntykwaBold.ttf'}
}
Note: you don't need to quote the key names in CFML, but ColdFusion will convert them all to upper case if you do not (I don't think Railo does, and there's a setting in CF11 to stop this from happening too). Note that in CF the ordering of the keys in a struct is not preserved; it can be in Railo, if using a linked struct (I'll leave it to you to look up about that)
You've a couple of options for looping over arrays and structs in CFML:
for (element in array){
}
for (key in struct){
value = struct[key];
}
One can also use iteration functions:
array.each(function(index, value, array){
});
struct.each(function(key, value, struct){
});
There are also other iteration methods such as filter()
, map()
and the like. There are new to Railo 4.2 and COldFusion 11. Previous versions of each had headless functions for each()
, eg: arrayEach(array, callback)
and structEach(struct, callback)
It's all in the docs.
Upvotes: 5