Good evening... I would like to pass array values as options to an array key (options).
This is what I would like to achieve:
array(
'name' => 'Select',
'id' => 'select',
'type' => 'select',
'options' => array( // Array of value => label pairs for radio options
'value1' => 'Label 1',
'value2' => 'Label 2'
),
),
Can't seem to pass a variable as options values. Example
$myarray = array('value1' => 'Label 1', 'value2' => 'Label 2')
array(
'name' => 'Select',
'id' => 'select',
'type' => 'select',
'options' => $myarray
),
The plugin I am working with is Taxonomy Meta from http://www.deluxeblogtips.com/taxonomy-meta-script-for-wordpress. Thanks in advance
You need to use proper PHP syntax and it will work just fine. Try this
<?php
$myarray = array('value1' => 'Label 1', 'value2' => 'Label 2');
$arr = array(
'name' => 'Select',
'id' => 'select',
'type' => 'select',
'options' => $myarray
);
print_r($arr);
I have a solution for it. I wasn't doing anything wrong except that I created the variable outside the function that created the array.
All I did was to create the variable inside the function that created the array and it worked. Thanks #RiggsFolly.
Related
I have an array where all arrays where key type == 'foo' must be replaced by a custom arrays.
Basically I need to find a specific array and then replace it with other arrays.
The issue here you can easily replace one array but when you insert numbers of arrays you are shifting keys so the next array type == 'foo' will not be replaced
Any help would be appreciated.
Here's what I have:
$array = array(
array(
'options' => array(
array(
'type' => 'foo'
),
array(
'type' => 'foo'
),
array(
'type' => 'bar'
)
)
),
array(
'options' => array(
array(
'type' => 'bar'
),
array(
'type' => 'bar'
),
array(
'type' => 'foo'
)
)
),
);
And I have an array which should replace any array where type == 'foo'
$array_foo = array(
array(
'type' => 'custom'
),
array(
'type' => 'custom_2'
),
array(
'type' => 'anything'
),
);
Here is the desired output:
$array = array(
array(
'options' => array(
array(
'type' => 'custom'
),
array(
'type' => 'custom_2'
),
array(
'type' => 'anything'
),
array(
'type' => 'custom'
),
array(
'type' => 'custom_2'
),
array(
'type' => 'anything'
),
array(
'type' => 'bar'
)
)
),
array(
'options' => array(
array(
'type' => 'bar'
),
array(
'type' => 'bar'
),
array(
'type' => 'custom'
),
array(
'type' => 'custom_2'
),
array(
'type' => 'anything'
),
)
),
);
Thank you.
Here's a way using 2 nested foreach loops and array_merge() with a temporary array:
// Pass the array by reference
foreach ($array as &$sub) {
// Temporary array
$new_options = [];
// Loop through options
foreach ($sub['options'] as $opt) {
// if type foo: replace by $array_foo items
if ($opt['type'] == 'foo') {
$new_options = array_merge($new_options, $array_foo);
// else, keep original item
} else {
$new_options[] = $opt;
}
}
// replace the options
$sub['options'] = $new_options;
}
And check the output:
echo '<pre>' . print_r($array, true) . '</pre>';
See also Passing by Reference
According to a previous post, the code for changing could be done in a similar fashion to the following code:
$base = array("orange", "banana", "apple", "raspberry");
$replacements = array(0 => "pineapple", 4 => "cherry");
$replacements2 = array(0 => "grape");
$basket = array_replace($base, $replacements, $replacements2);
print_r($basket);
So with your method instead of having numbers or indexes, I think you might be able to get away with writing down foo instead to get the replacement to work properly.
The other thing you might look into is array merge-recursive which can be found at: http://php.net/array_merge_recursive
The problem you stated here looks similar to this post: PHP Array Merge two Arrays on same key
I hope this helps you.
Is there any way to configure Zend\Form\Element\Select to return integer?
If I have a Form with a Select Element something like this (this is the common way to configure Select Element according to documentation and my internet research):
$this->add(array(
'name' => 'category_id',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Category',
'value_options' => array(
'1' => 'Gold',
'2' => 'Silver',
'3' => 'Diamond',
'4' => 'Charm'
),
'attributes' => array(
'class' => 'form-control',
),
));
I thought If I change value option like this:
$this->add(array(
'name' => 'category_id',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Category',
'value_options' => array(
1 => 'Gold',
2 => 'Silver',
3 => 'Diamond',
4 => 'Charm'
),
'attributes' => array(
'class' => 'form-control',
),
));
the integer will be returned but I was wrong. In both cases string is returned. My php code write this form values to a db table where category_id is defined as int.
In ZF2 use the Zend\Filter\Int or Zend\Filter\ToInt depending on which version of ZF2 you are using, Zend\Filter\Int became deprecated in ZF2.4.
In your form, assuming you are using the Zend\InputFilter\InputFilterProviderInterface use:
public function getInputFilterSpecification()
{
return array(
'category_id' => array(
'required' => TRUE,
'filters' => array(
array('name' => 'Int'),
),
'validators' => array(
// Your validators here
),
),
);
}
I have a array (generated by a function) that contains arrays and I want to add them to another array.
foreach ($elements as $element) {
$array[] =
array(
'name' => 'name_'.$element->name,
'desc' => 'Stuff for for '.$element->desc,
'attributes' => array(),
'type' => 'textarea'
);
}
And I have this:
$settings['var'] = array(
array(
array(
'name' => 'Test 1',
'desc' => 'This is test 1',
'attributes' => array(),
'type' => 'textarea'
),
array(
'name' => 'Test 2',
'desc' => 'This is test 2',
'attributes' => array(),
'type' => 'textarea'
),
),
);
Now I want to add all the elements in the first array (array[]) into the settings array.
If I do it like:
$settings['var'] = array(
array(
array(
'name' => 'Test 1',
'desc' => 'This is test 1',
'attributes' => array(),
'type' => 'textarea'
),
$array[0],
$array[1]...
it works, but I want to add all arrays in $array[] to the array.
I can't use a foreach here, so how can I do that?
To get the result you want, you can use array_merge inside the array:
$settings['var'] = array( array_merge(array1, array2))
the elements of array1 and array2 are arrays in your case.
Please refer to documentation for more details.
I have a ZF2 form where I had to disable native validators, for a specific reason.
Then, when adding elements programatically to the form I also add validators.
One of the elements is a Multiselect array.
$form->add( array(
'type' => 'Zend\Form\Element\Select',
'options' => array(
(
'label' => 'few items',
'value_options' => Array
(
'one' => 'one',
'two' => 'two',
'three' => 'three',
'four' => 'four',
)
),
'attributes' => array
(
'multiple' => 'multiple',
'value' => array('two','three'),
'required' => 1,
'id' => 'few_items'
),
'name' => 'few_items'
));
Also, I'm going to add an InArray validator:
if($f instanceof \Zend\Form\Element\Select){
$inputFilter->add($factory->createInput(array(
'name' => $f->getName(),
'required' => $f->getAttribute('required') == 1,
'validators' => array(
array(
'name' => 'InArray',
'options' => array(
'haystack' => $f->getValueOptions(),
'messages' => array(
InArray::NOT_IN_ARRAY => 'Please select an option',
),
),
),
),
)));
}
The problem is that the validator always fails, because in POST multiselect field will return an array, and actually looking inside the InArray validator, it uses in_array(...) PHP function which is not suitable for this - array_intersect would do the trick, but before writing my own validator I do have a feeling that this wheel was invented already!
Having looked around I see that there was a bug raised to this effect (http://framework.zend.com/issues/browse/ZF2-413), and the solution was to introduce Explode validator, but I'm not sure how to add it into my input filter.
Thanks for your suggestions.
Actually, following the bugfix link, I figured out how to do the validation. Explode validator would break down the value and apply a validator to each part:
if($f instanceof \Zend\Form\Element\Select){
$inputFilter->add($factory->createInput(array(
'name' => $f->getName(),
'required' => $f->getAttribute('required') == 1,
'validators' => array(
array(
'name' => 'Explode',
'options' => array(
'validator' => new InArray(array(
'haystack' => $f->getValueOptions(),
'valueDelimeter' => null,
'messages' => array(
InArray::NOT_IN_ARRAY => 'Please select an option',
),
))
)
),
),
)));
}
Leaving this question here, because I haven't found any other answers to this myself and hopefully this will assist people in the future.
I'm getting Validation error messages with
$workers = array('Filters' => array(array('Name' => 'Tags', array(array('Key' => 'Name', 'Values' => array('mworker'))))));
$list = $ec2Client->describeInstances($workers);
Error Detail:
[Filters][0][Filter] must be an array of properties. Got a numerically indexed array.'
This is with SDK2
Found the correct syntax:
$workers = array(array('Filters' => array('Name' => 'Tags', array('Key' => 'Name', 'Values' => array('mworker')))));
That actually didn't work for me - it seems the correct way to search by tags is:
$response = $ec2->describeInstances(array(
'Filters' => array(
array('Name' => 'instance-type', 'Values' => array('m1.small')),
array('Name' => 'tag-value', 'Key' => 'Name', 'Values' => array('Testing'))
)
));
Which also has a filter on instance-type to show how they are combined.