This is what I have:
'options' => array(
'active' => (count($panels)>=2) ? false : NULL,
'collapsible' => true,
'icons' => null,
'header' => "dt"
),
I assumed that the null on the value will be enough. However, the effect we wish will only occur, if and only if, all key=>value pair don't appear on the array.
How can I make this key=>value pair to appear:
'active' => false;
If count($panels)>=2 and to not appear at all, if that's not the case?
Is there an clear, easy and understandable way to achieve this, or should I play with array merges and stuff like that?
Please advice
I'd write it as follows:
$options = array(
'collapsible' => true,
'icons' => null,
'header' => "dt"
);
if (count($panels) >= 2) {
$options['active'] = false;
}
You'll obviously need to adapt this because your options array is contained within another.
well... something like this!
$array = array(
'options' => array(
'collapsible' => true,
'icons' => null,
'header' => "dt"
)
);
if (count($panels) >=2)
$array['options']['active'] = false;
and i know you will read this and go 'yes i couldve figured that out' but there is no other way that i know of.
Setting a value to null still creates a key. If you don't want the key, then only set the value in the cases you want:
$array['options'] = array(
...
);
if (count($panels) >= 2) {
$array['options']['active'] = false;
}
Reference:
'options' => array(
//... other key/val pairs
) + (count($panels)>=2 ? array('active' => false) : array()),
Related
I'm pulling data from a csv and uploading it to a db, for some columns, only certain values should be inputted (or it'll break going through the rest of the system)
however users don't to write out the full input so will use "y" instead of "yes"
I wrote a function that takes an array of the possible inputs and what the correct output should be.
however, currently the array and function looks like this:
$this->acceptedInputMap = array (
'yes' => 'yes',
'y' => 'yes',
'true' => 'yes',
'no' => 'no',
'n' => 'no',
'false' => 'no',
'unknown' => 'unknown',
'unk' => 'unknown',
'' => 'unknown'
);
//these are in the parents class------------------------------------
protected function useAcceptedinput()
{
foreach ($this->columnData as $key => $element) {
if ($this->isExpectedInput($element)) {
$this->useMappedInput($key, $element);
} else {
$this->useColumnDefault($key);
}
}
}
protected function isExpectedInput($element)
{
return array_key_exists(strtolower($element), $this->acceptedInputMap);
}
protected function useColumnDefault($key)
{
$this->columnData[$key] = $this->defaultValue;
}
protected function useMappedInput($key, $element)
{
$this->columnData[$key] = $this->acceptedInputMap[strtolower($element)];
}
I wanted to change this to use a different structure:
$this->acceptedInputMap = array (
'yes' => array(
'yes',
'y',
'true'
),
'no' => array(
'no',
'n',
'false'
),
'unknown' => array(
'unknown',
'unk',
''
),
);
This is a lot clearer for future developers and would make it a lot easier to add more accepted inputs.
This also allows me to have the parent store some common acceted inputs, such as "yes", that can be pulled down for each column that requires it.
However, this is a 2d array, and attempting to find the value in the second dimension to map to the correct input is more computationally intensive.
Is the change worth it?
Also, in general, where do you draw the line on making it easier for the next dev, vs making it faster?
You could organize the original array to make it more readable without sacrificing performance by adding comments or blank lines:
$this->acceptedInputMap = array (
// yes --------
'yes' => 'yes',
'y' => 'yes',
'true' => 'yes',
// no ----------
'no' => 'no',
'n' => 'no',
'false' => 'no',
// unknown -----
'unknown' => 'unknown',
'unk' => 'unknown',
'' => 'unknown'
);
I have a multi array and I need to combine it with other. This array is made for send in XML format to SOAP, so it need to have the correct structure.
This array is like an invoice, it have "items" which i have to repeat. So, i think in make two arrays (one have always the same structure) and add the items array.
The problem is that if I use merge, I could not put the second array in the correct key. Here an example.
This is the correct array structure:
$params = array(
'authRequest' =>
array( 'token' => 'token',
'sign' => 'sign',
'cuitRepresentada' => 'CUIT' ),
'comprobanteRequest' =>
array( 'codigoTipoComprobante' => $codtipcbte,
'numeroPuntoVenta' => $ptovta,
'numeroComprobante' => $cbte,
**'arrayItems' =>
array( 'item' =>
array(
array(
'tipo'=> $compreqitem['tipo'],
'codigoTurismo'=> $compreqitem['codTur'],
'descripcion'=> $compreqitem['descrip'],
'codigoAlicuotaIVA'=> $compreqitem['codAlic'],
'importeIVA'=> $compreqitem['impIva'],
'importeItem'=> $compreqitem['impItem'],
),
array(
'tipo'=> $compreqitem['tipo'],
'codigoTurismo'=> $compreqitem['codTur'],
'descripcion'=> $compreqitem['descrip'],
'codigoAlicuotaIVA'=> $compreqitem['codAlic'],
'importeIVA'=> $compreqitem['impIva'],
'importeItem'=> $compreqitem['impItem'],
),
),
),**
'arraySubtotalesIVA' =>
array( 'subtotalIVA' =>
array(
'codigo'=> $compreqiva['codIva'],
'importe'=> $compreqiva['importe'],
),
),
),
);
So, i build the array with "arrayItems" empty
'arrayItems' => array(),
Then i build the arrayItem array:
$arrayitems =
array('arrayItems' =>
array( 'item' =>
array(
array(
'tipo'=> $compreqitem['tipo'],
'codigoTurismo'=> $compreqitem['codTur'],
'descripcion'=> $compreqitem['descrip'],
'codigoAlicuotaIVA'=> $compreqitem['codAlic'],
'importeIVA'=> $compreqitem['impIva'],
'importeItem'=> $compreqitem['impItem'],
),
array(
'tipo'=> $compreqitem['tipo'],
'codigoTurismo'=> $compreqitem['codTur'],
'descripcion'=> $compreqitem['descrip'],
'codigoAlicuotaIVA'=> $compreqitem['codAlic'],
'importeIVA'=> $compreqitem['impIva'],
'importeItem'=> $compreqitem['impItem'],
),
),
),
);
Then i use merge to join both array:
$resultado = array_merge($params['comprobanteRequest'], $arrayitems);
Works, but the first key is deleted...
'authRequest' =>
array( 'token' => 'token',
'sign' => 'sign',
'cuitRepresentada' => 'CUIT' ),
I dont know why is deleted, maybe the merge function is not the corect way...
Thanks in advance!
If in your first array, arrayItems is always empty, then you don't need a merge, just set the value :
$params['comprobanteRequest']['arrayItems'] = $arrayItems['arrayItems'];
Of course this can be simplified, since $arrayItems contains only one key, but you get the spirit.
I want to filter an array over Hash::filter and use a callback function
static Hash::filter(array $data, $callback = array('Hash', 'filter'))
...You can also supply a custom $callback to filter the array elements...
(CakePHP Docs)
My question here is just... How?
Maybe there's a failure in my head with the translations, but i have the JavaScript filter function in mind, where you can filter over an array and give the filterfunction the actual element its iterating over atm. Then if it returns false it gets kicked out of the array.
maybe im just bad with php but.. could anybody help me with it, please? :)
my attempt atm is something like this
$bis_datum = '2017-01-01';
$res = Hash::filter($multidim_assoc_array, function($part_of_multidim_assoc_array){
return !strtotime($assoc_array['von_datum']) > strtotime($bis_datum);
});
i know there's something very wrong here, because it sais
array('Hash', 'filter')
in the docs and theres just an anonymous function here, but i dont get what the "Hash" and "filter" part means :S
$example = array(
'User' => array(
0 => array(
'name' => 'Bob',
'age' => 25
),
1 => array(
'name' => 'John',
'age' => 22
),
2 => array(
'name' => 'Jen',
'age' => 32
)
)
'School' => array(
'name' => 'Brainslaves High',
'adress' => 'Somestreet 42'
)
);
as an easy example.. how can i filter this array to kick out everyone whos age is below 25 ?
Thanks-a-lot!
Hash::filter won't help you for your example, you better work with array_filterdirectly
$res = array('User' => array_filter($example['User'], function($user) {
return $user['age'] > 25;
})) + array('School' => $example['School']);
Imagine this situation:
$component = array(
'type' => 'chimney',
'material' => 'stone'
);
What i would like to do is to add a key/value pair to this array, if a certain condition is met.
$hasMetrics = true;
$component = array(
'type' => 'chimney',
'material' => 'stone',
'metrics' => ($hasMetrics ? array('width' => 60, 'height' => 2000) : false)
);
While this could be used, it will always cause a key called 'metrics' in my array.
Of course, if i don't want that, i could use array_merge() to merge a second array with the first (the second being either an empty array or the desired key/value pair, depending on the condition).
But what i am longing to find out is if there is any way to define this array like above, while taking care of $hasMetrics, without the use of any other means (such as array_merge()) but purely in the actual (first and only) definition of this array.
Like this: (non-applicable, demonstrative example)
$component = array(
'type' => 'chimney',
'material' => 'stone',
($hasMetrics ? array('metrics' => array(
'width' => 60,
'height' => 2000
)) : false)
);
(This, as i understand it, would generate two keys (type and material and then create one keyless value that is, itself, an array containing a key (metrics) and another array as value.)
Can anyone show me some proper approach? Perhaps there is some kind of PHP function available, with special properties (such as list() which is capable of cross-assignment).
EDIT
Perhaps some more clarification is needed, as many answers point out ways to go such as:
Using a followup assignment to a certain key
Filtering the generated array after defining it
While these are perfectly valid ways to extend the array, but i am explicitly looking for a way to do this in one go within the one array definition.
Not with the array defenition itself. I would add it to the array if necessary:
if($hasMetrics) {
$component['metrics'] = array('width' => 60, 'height' => 2000);
}
$hasMetrics = true;
$component = array(
'type' => 'chimney',
'material' => 'stone',
);
if($hasMetrics){
$component['metrics'] = array('width' => 60, 'height' => 2000);
}
Try
$component = array(
'type' => 'chimney',
'material' => 'stone',
'metrics' => $hasMetrics ? array('width' => 60, 'height' => 2000) : ''
);
And after that
$component = array_filter( $component ); // remove if it has '' value
OR
$component = array(
'type' => 'chimney',
'material' => 'stone',
);
if($hasMetrics) {
$component['metrics'] = array('width' => 60, 'height' => 2000);
}
I am trying to get the value from a custom category attribute in Magento. The attribute is a select field and is been made with the install script below:
$this->startSetup();
$this->addAttribute('catalog_category', 'category_categorycolor', array(
'group' => 'General Information',
'input' => 'select',
'type' => 'varchar',
'label' => 'Categorie kleur',
'backend' => '',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'option' => array (
'value' => array('yellow' => array('Geel'),
'purple' => array('Paars'),
'blue' => array('Blauw'),
'red' => array('Rood'),
'orange' => array('Oranje'),
'green' => array('Groen'),
'darkblue' => array('Donkerblauw'),
'lightgreen' => array('Lichtgroen'),
)
),
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$this->endSetup();
Unfortunately only getting numbers and not text value. I use this line to retrieve the value:
<?php $_category_categorycolor = $_category->getData('category_categorycolor'); if($_category_categorycolor): ?> <?php echo $_category_categorycolor; ?> <?php endif; ?>
Can someone help me?
Something like this:
$category_id = '10';
$attribute_code = 'category_categorycolor';
$category = Mage::getModel('catalog/category')->load($category_id);
echo $category->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($category);
The sollution is pretty messy (the only one I know of).
$opt = array(); // will contain all options in a $key => $value manner
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_category', 'category_categorycolor');
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
foreach ($options as $o) {
$opt[$o['value']] = $o['label'];
}
}
$categoryColorId = $_category->getData('category_categorycolor');
$categoryColorLabel = $opt[$categoryColorId];
// if you have problems, do a Zend_Debug::dump($opt);
// - it should contain an array of all the options you added
Didn't test it out, let me know if it works or not.
PS: can't reply to your comment, not sure why. What does $opt contain ?
The numbers you are getting back are the id's of each value in the dropdown. You have to load the dropdown values too.
See the following page. It helped me understand this.
http://www.sharpdotinc.com/mdost/2009/04/06/magento-getting-product-attributes-values-and-labels/