I am trying to show a form based on a variable in the url. This is my array:
$blocks = array(
'oc1' => array(
'slugid' => 'oc1',
'title' => 'One Column 1',
'desc' => 'Block with text',
'values' => array(
'textarea',
'title'
)
),
'oc2' => array(
'slugid' => 'oc2',
'title' => 'One Column 2',
'desc' => 'Block with button',
'values' => array(
'title'
)
)
);
Now I want to show form fields based on the values array. So if my url is test.php?b=oc1 it should show the textarea field. If test.php?b=oc2 it should not because textarea is not added to the values array.
I've tried a lot of answers I found on StackOverflow but I can't get it to work.
So if anyone knows how to do this I would be very very grateful.
Check if is defined the $_GET variable (if you have not done it before) and using the in_array function check if textarea value exist in your two-dimensional array.
if (isset($_GET['b']) && in_array('textarea', $blocks[$_GET['b']]['values']))
{
echo 'textarea';
}
Related
This question already has answers here:
PHP is there a way to add elements calling a function from inside of array
(3 answers)
Closed last month.
In the middle of declaring an array of arrays, I want to "write" an array of arrays generated by my function.
I have a working example when I:
simply store my function-generated arrays into a variable and
then call each array from that function by its key,
but I can't find a command to simply call everything at once.
Here is the code which (I hope) explains it:
<?php
// A. (THIS WORKS)
// A1: A function that returns an array of arrays
function my_arrays_building_function() {
$first_array = array(
'id' => 'my_array_1',
'type' => 'some-type',
'title' => 'My title 1',
);
$second_array = array(
'id' => 'my_array_2',
'type' => 'some-type',
'title' => 'My title 2',
);
// ... and so on, many more.
return array(
'first-array' => $first_array,
'second-array' => $second_array,
// ... and so on.
);
// NOTE there are tens or hundreds of returned arrays here.
}
// A2: Store my arrays in a variable
$my_array = my_arrays_building_function();
// A3: Inside an array (of arrays), I simply "write" my arrays INDIVIDUALLY and THAT works
array(
array(
'id' => 'dummy_preexisting_array_1',
'type' => 'some-type',
),
array(
'id' => 'dummy_preexisting_array_2',
'type' => 'some-type',
),
// HERE THERY ARE, INDIVIDUALLY, COMMA SEPARATED
$my_array[ 'first-array' ],
$my_array[ 'second-array' ],
array(
'id' => 'dummy_preexisting_array_n',
'type' => 'some-type',
)
),
/** -------------------- //
THE ISSUE
// -------------------- **/
// B: HOW DO I "write" THEM ALL AT ONCE???
// B1: The same as A1
function my_arrays_building_function() {
$first_array = array(
'id' => 'my_array_1',
'type' => 'some-type',
'title' => 'My title 1',
);
$second_array = array(
'id' => 'my_array_2',
'type' => 'some-type',
'title' => 'My title 2',
);
// NOT SURE I SHOULD RETURN LIKE THIS
return array(
'first-array' => $first_array,
'second-array' => $second_array
);
}
// B2: Same as A3, Inside an array (of arrays), I "write" my arrays BUT NOW I WANT TO "WRITE" THEM ALL AT ONCE
array(
array(
'id' => 'dummy_preexisting_array_1',
'type' => 'some-type',
),
array(
'id' => 'dummy_preexisting_array_2',
'type' => 'some-type',
),
/** >>>> I need my arrays here ALL AT ONCE aka NOT INDIVIDUALLY AS IN EXAMPLE A. <<<< **/
/**
* In other words, while I'm declaring this array,
* I simply need all my arrays from my_arrays_building_function()
* "written" here with a simple command instead of calling hundreds
* of arrays individually as in the first example
*/
array(
'id' => 'dummy_preexisting_array_n',
'type' => 'some-type',
)
), /* this goes on as it's a part of even bigger array */
Although I wouldn't recommend declaring hundreds of array variables inside a function because that's crazy, but for now, you can use get_defined_vars() to get over this issue.
You will also need to filter out the variables which are arrays and has the keys id, type and title as there are could be several other variables defined apart from this.
Snippet:
<?php
array_filter(get_defined_vars(), fn($val) => is_array($val) && isset($val['id'], $val['type'], $val['title']));
Online Demo
Not too sure if I'm understanding this correctly but from what I assume, you just want to return an array with a bunch of others inside it that you define throughout the function?
A simple approach for this would be to define your output variable immediately and add all of your other arrays to it:
function my_arrays_building_function() {
$output = [];
$output[] = [
'id' => 'my_array_1',
'type' => 'some-type',
'title' => 'My title 1',
];
$output[] = [
'id' => 'my_array_2',
'type' => 'some-type',
'title' => 'My title 2',
];
return $output;
}
Thank you to #Rylee for suggesting Array Unpacking.
The final code would look like this:
// C1: A function that returns an array of arrays BUT without keys
function my_arrays_building_function() {
$first_array = array(
'id' => 'my_array_1',
'type' => 'some-type',
'title' => 'My title 1',
);
$second_array = array(
'id' => 'my_array_2',
'type' => 'some-type',
'title' => 'My title 2',
);
// ... and so on, many more.
// Then return but now I don't assign keys, I only list the vars.
return array(
$first_array, $second_array, ... and so on.
);
}
// C2: Inside an array (of arrays), I use Array Unpacking, THAT'S WHAT I WAS LOOKING FOR, UNPACK ARRAY! SEE BELOW
array(
array(
'id' => 'dummy_preexisting_array_1',
'type' => 'some-type',
),
array(
'id' => 'dummy_preexisting_array_2',
'type' => 'some-type',
),
// HERE I UNPACK MY ARRAY BY USING ... THE THREE DOTS ARE THE KEY
... my_arrays_building_function(),
array(
'id' => 'dummy_preexisting_array_n',
'type' => 'some-type',
)
),
Hooray!
I'm editing a Woocommerce function to display different texts in different languages. I'm not familiar with objects, hence the call for help.
I need to insert a variable into a multidimensional array, but since it is an object, I have not been able to do it via array_push.
My goal is to insert the value of $comments_placeholder into the array key 'placeholder'.
Here's the current code:
$this->fields = array(
'billing' => WC()->countries->get_address_fields(
$billing_country,
'billing_'
),
'shipping' => WC()->countries->get_address_fields(
$shipping_country,
'shipping_'
),
'account' => array(),
'order' => array(
'order_comments' => array(
'type' => 'textarea',
'class' => array( 'notes' ),
'label' => __( 'Order notes', 'woocommerce' ),
'placeholder' => '',
),
);
array_push($this["order"]["order_comments"]["placeholder"]) = $comments_placeholder;
Thanks!
$this->fields['order']['order_comments']['placeholder']=$comments_placeholder
$this->fields refers to the array of the object, instead of $this, which refers to the object itself. Since 'placeholder' is a key and not an array, instead of array_push which appends a value to an array, you can assign the value of $comments_placeholder to the key 'placeholder' instead.
I am adding some custom fields to woocommerce checkout using woocommerce_checkout_fields filter. One of those fields is a select dropdown. This is my code for the fields.
// Add a new checkout field
function ds_filter_checkout_fields($fields){
$suburb = ds_get_delivery_suburbs();
$postcodes = ds_get_delivery_postcodes();
$fields['extra_fields'] = array(
'some_field' => array(
'type' => 'text',
'required' => true,
'label' => __( 'Some field' )
),
'select_field' => array(
'type' => 'select',
'options' => array('key' => 'value'),
'required' => true,
'label' => __( 'Another field' )
)
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'ds_filter_checkout_fields' );
If you check the select_field code there is a options property and it takes key and value pair ... I want to insert dynamic key and dynamic value to options property ... In the code I am getting the dynamic key from $postcodes and dynamic value from $suburb and when I try to insert it like this 'options' => array($postcodes => $suburb), I get this warning Warning: Illegal offset type ... I have tried couple of other methods but they didn't work so I turned to you guys ... I appreciate your help ... looking forward to your responses.
NOTE: I have googled this but haven't found any answers so that is why I turned to Stackoverflow for help.
If you want it to be dynamic you have to first set and fill the array and then use it, so it would be
// Add a new checkout field
function ds_filter_checkout_fields($fields){
$suburb = ds_get_delivery_suburbs();
$postcodes = ds_get_delivery_postcodes();
$ma_options[$postcodes] = $suburb;
$fields['extra_fields'] = array(
'some_field' => array(
'type' => 'text',
'required' => true,
'label' => __( 'Some field' )
),
'select_field' => array(
'type' => 'select',
'options' => $ma_options[$postcodes],
'required' => true,
'label' => __( 'Another field' )
)
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'ds_filter_checkout_fields' );
If it doesn't work then share var_dump($ma_options);
--- UPDATE
That could happen because of index not existing or passing something which isn't an string as the key. You should cast your type to string if desired efect, or use stdClass object.
Maybe the type of my question is not quite clear, but i don't know a better way to explain what i need. Here is the thing i am working on some custom page in WordPress and i have input fields in an array. The input fields in that array match the id's of second array, but i actually need to get the type field of second array that match the id of first array.
Here is example of first array
$first_array = array(
'sample_text' => 'some text here',
'sample_textarea' => 'some text here in textarea field',
'sample_upload' => 'http://somelink.com/someimage.jpg'
);
And here is the second array.
$second_array = array(
array(
'type' => 'upload',
'id' => 'sample_upload',
'title' => 'Sample upload button'
),
array(
'type' => 'textfield',
'id' => 'sample_text',
'title' => 'Sample text field'
),
array(
'type' => 'textarea',
'id' => 'sample_textarea',
'title' => 'Sample textarea field'
),
);
So basically the second array is used in first place to generate an input fields in front-end, but upon form submit the form submits an array which looks like first example, so now on first array i need to loop for each input and match the id's of second and first array but when id's are match i need to take the type field and apply filter with that type field name.
So basically
// loop through inputs in the array
foreach( $first_array as $key => $value ) {
// Now the first $key would be 'sample_text'
// How to search $second_array for 'id' with 'sample_text'
// And if that 'id' exists, take the 'type' field and apply filter named same as that 'type' field
}
But i don't know exactly how i would loop through second array and get 'type' based on 'id'
I would add useful keys to the second array, like so:
$second_array = array(
'sample_upload' => array(
'type' => 'upload',
'id' => 'sample_upload',
'title' => 'Sample upload button'
),
'sample_text' => array(
'type' => 'textfield',
'id' => 'sample_text',
'title' => 'Sample text field'
),
'sample_textarea' => array(
'type' => 'textarea',
'id' => 'sample_textarea',
'title' => 'Sample textarea field'
),
);
When looping over the first array, you can use the known keys to access the second array.
foreach ($first_array as $key => $value) {
$sa = $second_array[$key];
}
That loop would usually have some more error-checking code in there, e.g. to make sure that the key exists, which has been left out for the sake of brevity.
Edit: array_filter() won't work as original though, so you could use the following instead
function checkKey($item, $k, $key) {
return $item['id'] === $key;
}
foreach( $first_array as $key => $value ) {
// Now the first $key would be 'sample_text'
// How to search $second_array for 'id' with 'sample_text'
$sa = $second_array[array_walk($second_array, 'checkKey', $key)];
// And if that 'id' exists, take the 'type' field and apply filter named same as that 'type' field
}
When I try to edit custom_fields with wp.editPost. Only edit the other fields, but not custom fields. Custom fields are created again(repeat fields), but will have to be edited.
I am looking: http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.editPost
My array with custom fields is:
$content = array(
'post_id' => (int)$idPostWp,
'title' => $modificarPostWpDecode['title'], //ok edit
'description' => $modificarPostWpDecode['content'], //ok edit
'categories' => $modificarPostWpDecode['category'], //ok edit
'custom_fields' => array(
array('key' => 'precio', 'value' => $modificarPostWpDecodeCustom['price']), // no edit, fields will be create again
array('key' => 'category', 'value' => $modificarPostWpDecodeCustom['category']), // no edit, fields will be create again
array('key' => 'estrenar', 'value' => $modificarPostWpDecodeCustom['new']), // no edit, fields will be create again
array('key' => 'currency', 'value' => $modificarPostWpDecodeCustom['currency']), // no edit, fields will be create again
array('key' => 'search', 'value' => $modificarPostWpDecodeCustom['search']) // no edit, fields will be create again
)
);
My call to wordpress is:
$params = array(1, WPUSER, WPPASS, (int)$idPostWp, $modificarPostWpDecode);
$request = xmlrpc_encode_request('wp.editPost', $params, array('encoding' => 'UTF-8', 'escaping' => 'markup'));
Thank a lot!
As noted here, you must pass the custom field's ID to edit the field, rather than the key, which would end up creating a duplicate.
So you need to do two requests, unless you already know the custom field IDs. One request to get all the custom data, loop through the fields, and collect the appropriate IDs to the fields you want to update. The second request will update the fields, specified using the field IDs, not just the key.
The collection of IDs can look similar to the following
$custom_fields_to_edit = array(
'key1' => null,
'key2' => null
);
foreach($post->custom_fields as $custom){
if (array_key_exists($custom->key, $custom_fields_to_edit)){
$custom_fields_to_edit[$custom->key] = $custom->id;
}
}
where $post has been collected using the wp.getPost procedure.
You can then proceed as previously, with the following modification to your code.
'custom_fields' => array(
array('id' => $custom_fields_to_edit['key1'], 'key' => 'key1', 'value' => $modificarPostWpDecodeCustom['key1']),
array('id' => $custom_fields_to_edit['key2'], 'key' => 'key2', 'value' => $modificarPostWpDecodeCustom['key2'])
)