I'm trying to pass settings like this:
$mySettings = array(
'settingOne' => 'someValue',
'settingTwo' => 5,
'settingThree' => true
);
from the view into an element like so:
echo $this->element('foobar', $mySettings);
How would I set the default values for them inside of the element?
Or is it better to set them somewhere else? If so, where and how?
Thank you.
Write default code in appsController like below
$mySettings = array(
'settingOne' => 'someValue',
'settingTwo' => 5,
'settingThree' => true
);
$this->set('foobar', $mySettings);
And If you want to Modify $mySettings then you have to write same code in Controller that you want to change from.
You have to use foobar variable in element like
$foobar['settingOne'];
$foobar['settingTwo'];
$foobar['settingThree'];
if the setting entries are dynamic, you can try this:
function element($entry, $settings, $default = null) {
if (isset($settings[$entry])) {
return $settings[$entry];
} else {
return $default;
}
}
if the setting entries are static, you'd better define a class, each entry as a property.
The keys you pass in are transformed into variables
So just do
if (!isset($theConfigKey)) {
$theConfigKey = ...
}
Related
I have a Stripe subscription object that looks like this...
subscription: {
items: {
data: [
plan: {
id: 'my_plan_id'
}
]
}
}
What's the best way to safely retrieve the plan id? Currently I am doing the following.
'plan_id' => $subscription->items->data[0]->plan->id,
But, it looks like that will fail if items, data[0], or plan, is not set. I could do nest if statements like, if (isset($subscription->items) && isset(data[0]) ..., but I am not sure that is the best way.
Is there a PHP method or Laravel method that I can use to extract that property safely that would be cleaner than that?
If you're using PHP 7+, you can use the null coalesce operator:
'plan_id' => $subscription->items->data[0]->plan->id ?? $default,
This will evaluate the value if it's available, otherwise it will use the default, without generating any warnings or errors.
Example:
$foo = new stdClass();
var_dump($foo->bar->baz->data[0]->plan->id ?? null);
Output:
NULL
You can use an isset function over the entire selector:
isset($subscription->items->data[0]->plan->id) ? $subscription->items->data[0]->plan->id : null;
in PHP version 8
you should use Nullsafe operator as follow:
'plan_id' => $subscription?->items?->data[0]?->plan->id,
A rather cumbersome but generic method to access nested structures by a list of keys; can be made into a reusable function easily:
$id = array_reduce(['items', 'data', 0, 'plan', 'id'], function ($o, $k) {
if (!$o) {
return null;
} else if (is_array($o) && isset($o[$k])) {
return $o[$k];
} else if (isset($o->$k)) {
return $o->$k;
}
}, $subscription);
you can take help of ternary condition to check if the value is set or not. If set then put that value otherwise put default as
$id=isset($subscription->items->data[0]->plan->id) ? $subscription->items->data[0]->plan->id : $your_dafault_value;
If the value is set then the $id looks like
$id='my_plan_id';//In most cases we should consider null as a default value
If the value is not set then the $id looks liks
$id='your_dafault_value';
I have stored the session varible in one controller and getting the value in another controller but the value is not passing
here is one controller
function control1 {
$this->session->set_userdata(array(
'value1' => $this->input->post('value1'),
'value2' => $this->input->post('value2'),
);
echo $this->session->userdata('value1'); //it returns value
}
function control2 {
echo $this->session->userdata('value1'); //it returns empty value
}
What may be the reason for this
You should check if the session has been set in the second controller like this:
function control2 {
if (!isset ($this->session->userdata('value1'))){
redirect('control1');
} else {
echo $this->session->userdata('value1'); //it returns empty value
}
If you haven't run through the control1 first, there won't be any session set yet, you see.
$this->session->set_userdata( 'values', array(
'value1' => $this->input->post('value1'),
'value2' => $this->input->post('value2'),
) );
$a = $this->session->userdata('values'); //it returns value
print_r( $a );
Try this code.
How the way you go into control2()? I mean, did you call control1() first to set the sessions value before goes to control2()? If so then the sessions value should be passed.
I needed a function that recursively parses a multi-dimensional array for a (part) of a certain string value and, if found, returns the entire value the string is contained in.
I came up with the following solution:
function & ransackArray(array & $haystack, $needle) {
foreach($haystack as &$element) {
if(is_array($element)){
if($v=ransackArray($element, $needle))
return $v;
} else {
if(strstr($element, $needle))
return $element;
}
}
return false;
}
This works fine. For instance, providing:
$data=array(
'key' => 'something',
'children' => array(
'someValue' => 'myTest',
'someMore' => 'yes'
)
);
And running:
$r=ransackArray($data, 'myTes');
This will result in $r containing 'myTest'.
The problem is that now that i found this value, i want to change it in $data, right on the fly, by writing:
$r='new value';
Which should then effectively result in data looking like this:
$data=array(
'key' => 'something',
'children' => array(
'someValue' => 'new value',
'someMore' => 'yes'
)
);
This however, doesn't seem to work. Perhaps i misunderstand something about references. In any case, the reason i needed this to work is why i pass $haystack as a reference and also return the function's result as one.
Can this be done? If yes, how? And if not - why not?
You're missing two ampersands...one on this line:
if($v = self::ransackArray($element, $needle))
which should be:
if($v = &self::ransackArray($element, $needle))
and one on this line:
$r = ransackArray($data, 'myTes');
which should be:
$r = &ransackArray($data, 'myTes');
(Note: it looks like your ransackArray function is actually a method in a class, so if you're calling that method from within the class it would be $r = &$this->ransackArray($data, 'myTes');)
When passing variables to a function, you don't need to use & - just put the & in front of the parameter in the function signature - but in cases like yours where you are getting a return value from a function, there needs to be a & both in the function call and in the function signature. For more info see http://www.php.net/manual/en/language.references.pass.php
In Python it is possible to have a function with several variables all having a default value. And then just passing the value of one of the values. So if I have
function foo(a=10,b=50, c=70)
pass
pass
return
Then I can call
foo(b=29)
and it would call
foo(10,29,70)
(using the default for all the values, and the exact value for that one variable).
Is something similar possible in PHP?
No there is no equivalent to that in PHP. You can have default values for function arguments, but they are evaluated from left to right and are not named:
function test($var1 = 'default1', $var2 = 'default2')
{
}
In that example the two variables are optional, but you must specify the first argument if you want to specify the second.
test(); // works
test('arg1'); // works
test('arg1', 'arg2'); // works
test('arg2'); // this will set the first argument, not the second.
A common workaround if you need flexibility on your optional arguments is to pass an array as the argument:
function test($options)
{
}
This can have a variable number of arguments in the form of a single associative array:
$options = array('var1' => 'arg1', 'var2' => 'arg2');
test($options);
Use array as an argument. For example:
function a(array $params) {
$defaults = array(
'a' => 10,
'b' => 50,
'c' => 70,
);
$params += $defaults;
// use $params
}
a(array('b' => 29));
I have a function that look like this:
function test($arg1 = 'my_value', $arg2 = 'second')
{
}
When I call it I only want to set the second value to something different, like this:
test(inherit, 'changed value');
I found out that it is possible to add this line to my function (when my "inherit" is changed to null):
$arg1 = ( is_null( $arg1 ) ? 'my_value' : $arg1 );
Is there a better way, a nicer way to solve it?
Depending on the nature and number of your parameters it may be reasonable to use named parameters (at least emulated):
function xyz($args) {
$args += array(
'x' => 'default',
'q' => 'default 2',
// ...
);
// ...
}
xyz(array('q' => 'hehe, not default'));
The way you have solved it is actually pretty usable.
The other way is to pass the same value as the default value every time on the function call.
If that is structural, then you have to reconsider the function.
Make two different functions:
// Full function
function testex($arg1 = 'my_value', $arg2 = 'second')
{
}
// Shorthand when just argument 2 is needed
function test2($arg2 = 'second')
{
return testex('my_value', $arg2);
}
That way, you don't have to pass null to the first parameter when you don't need to.
You will have to flip them, you can't leave the first value to be blank,
Set the first value and let the second one be the default value;
UPDATE:
If you want to have dynamic length to your argument consider using the func_get_args();
function something() {
$args = func_get_args();
....
}
then you can test your arguments for different value or datatype to make them do whatever yuo please