How to insert a variable in a multidimensional array inside a class - php

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.

Related

PHP Write "an array of arrays" dynamically inside an array of arrays, while declaring it [duplicate]

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!

Get value from a multidimensional array based on a parameter

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';
}

Dynamically add key value pair to options property of woocommerce checkout fields filter

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.

Find key type of second array by matching key id of second array with key id of first array

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
}

Conditionally adding item to multidimensional array

I have been looking how to do this and am a bit stumped.
My array is as follows:
$returndata->setup_array = array(
'General' => array(
'Main Details' => 'setup/maindets',
'Directories' => 'directories',
'Extension Allocation' => 'xtnallo',
'List Holidays' => 'setup/holidays',
'List Group VM' => 'groupvm',
'Conference Rooms' => 'confroom'
),
'Offices' => array(
'List Offices' => 'iptoffices'
),
'Users' => array(
'List Users' => 'iptusers'
),
'Phones' => array(
'List Phones' => 'iptphones'
),
);
However I have 1 item that on a certain condition(triggered by the users session) that needs to be added to the listin the general array. The section being 'View Details => setup/viewdetails'. I have tried array push (probably incorrectly) but this adds the item as another array at the end under the main array.
I want/need it to work like this:
$returndata->setup_array = array(
'General' => array(
$viewdets
'Main Details' => 'setup/maindets',
'Directories' => 'directories',
'Extension Allocation' => 'xtnallo',
'List Holidays' => 'setup/holidays',
'List Group VM' => 'groupvm',
'Conference Rooms' => 'confroom'
),
'Offices' => array(
'List Offices' => 'iptoffices'
),
'Users' => array(
'List Users' => 'iptusers'
),
'Phones' => array(
'List Phones' => 'iptphones'
),
);
$viewdets = "'View Details' => 'setup/viewdetails'";
and still be interpreted as a functioning array for use as a menu.
$returndata->setup_array['General']['View Details'] = 'setup/viewdetails'
Cheers Rick!
You can use ArrayObject to have the array as a reference:
$a = new ArrayObject();
$b = array(
"a" => $a
);
$a[] = "foo";
print_r($b);
What did you try calling array_push() on? Have you tried
array_push($returndata->setup_array['General'], $viewdets);
You would need to add the variable to the specific depth of the array you wanted it to be present. check out array_push here, there's also a short language syntax that avoids the function call:
$returndata->setup_array['General'][] = $viewdets;

Categories