Forums / Developer / Associative arrays not working?
Oliver Frommel
Thursday 07 August 2008 6:49:11 am
Hello,
yesterday I tried to extend the Collected information Export extension (http://projects.ez.no/cie) to export the ezoption datatype. In that course I discovered some strange behaviour where I could not reference an associative array as I thought I could.
class eZOptionHandler extends BaseHandler{ function exportAttribute(&$attribute, $seperationChar) { echo $array['DataInt']; ...
that last statement never produced any output, whereas var_dump($attribute) shows:
object(ezinformationcollectionattribute)(10) { ["PersistentDataDirty"]=> bool(false) ["Content"]=> NULL ["ID"]=> string(5) "15178" ["InformationCollectionID"]=> string(5) "11191" ["ContentClassAttributeID"]=> string(4) "1005" ["ContentObjectAttributeID"]=> &string(6) "172424" ["ContentObjectID"]=> &string(5) "20112" ["DataText"]=> string(0) "" ["DataInt"]=> string(1) "3" ["DataFloat"]=> string(1) "0" }
I have worked around that problem by iterating over the keys of the structure:
foreach ($attribute as $key => $value) { if ($key == 'DataInt') { $option_value = $value; } }
I remember somewhere in the back of my mind to have read something about variables/datatypes getting "loaded" from the db only "on demand". Otherwise I have no idea what is going on here. Can anonye explain?
ThanksOliver
André R.
Thursday 07 August 2008 7:00:48 am
From the var_dump ypu can see that this is a object of type eZInformationCollectionAttribute, not a array.
The clean way to get the variable would be (this is supported by all classes that are meant to be used inside a template):
$dataInt = $attribute->attribute('data_int');
Or the regular way:
$dataInt = $attribute->DataInt;
eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription @: http://twitter.com/andrerom
Thursday 07 August 2008 8:36:29 am
This is great, thanks.
Maybe you can also explain how I get the description of the options that are found in the content object instance belonging to the information collection? The only thing I could find was a text attribute filled with an XML fragment like this:
// <ezoption> // <name>company_size</name> // <options> // <option id="0" // additional_price="0">0-10</option> // <option id="1" // additional_price="0">10-20</option> // <option id="2" // additional_price="0">20-30</option> // <option id="3" // additional_price="0">30-40</option> // </options> // </ezoption> //
I was able to parse it but it is a bit cumbersome. I thought there was a way just looking the description up via an (associative) array?
Thursday 07 August 2008 9:13:55 am
Some template code for selection attribute that might help:
{def $portion_array = $node.data_map.portions_type.content $portion_type = ''} {foreach $node.data_map.portions_type.class_content.options as $option} {if $portion_array|contains( $option.identifier )}{set $portion_type = $option.name|wash(xhtml)}{/if} {/foreach}