I have an array:
array(
'myVar1' => 'value1',
'myVar2' => 'value1',
'myVar3' => 'value3',
);
Is there a built in function in PHP that will make 3 variables e.g. $myVar1, $myVar2, $myVar3 do that when i echo $myVar1; it retuens 'value1'
Obviously I can loop the array and set them accordingly (so please no answers with this), but if there is a internal PHP function that would be great!
extract() is the function:
Import variables into the current symbol table from an array...
Checks each key to see whether it has a valid variable name. It also checks for collisions with existing variables in the symbol table...
$array = array(
'myVar1' => 'value1',
'myVar2' => 'value1',
'myVar3' => 'value3',
);
echo $array['myVar1'];
echo $array['myVar2'];
Is that what you mean?
Related
I'm trying to see if you can assign an array value during creation to a value in the same array.
If I'm declaring and initializing an array like this:
$this->array = array(...);
Can I do something like this?
$this->array = array
(
'value1' => 'hello',
'value2' => $this->array['value1'],
);
I've tried it already and I get back
Notice: Undefined index: value1 in /Sites/website/config.php on line 329
I've gotten around this problem already but now out of interest I want to see if theres actually a way to do this.
It has to be during creation and declared like above, not through
$array['value1'] = 'foo';
$array['value2'] = $array['value1'];
or
$common = 'foo';
$array = array('value1' => $common, 'value2' => $common);
Is this technically impossible or is there any way?
I don't think it's possible. The array has to be parsed before it can be assigned, so at the time $this->array['value1'] is being calculated, $this->array is still null.
Basically, $this->array['value1'] cannot be be accessed before $this->array['value2'] is set, which needs to access $this->array['value1'], which cannot be accessed before . . .
The reason you're getting Notice: Undefined index: value1 in /Sites/website/config.php on line 329 is because when you're defining your array the value you're trying to access has not yet been defined because you haven't reached the end of the initial function.
In the following code:
$this->array = array
( //this is the start of the array
'value1' => 'hello',
'value2' => $this->array['value1'],
); //this is the end of the array statement.
The array is not defined until you reach the end of the statement.
The reason the other methods work is because you're using multiple steps to access an already existing variable.
I think that is not possible, assign the element when the array starts
you can set as false and before the action that set the value as your code.
example :
$common = 'foo';
$array = array(
'value1' => $common,
'value2' => $common
);
or you can use the function array_combine
Sorry for this question but I just curious with this case. Okay for example I have an array elements :
<?php
$data = array(
'key1' => 'val1',
'key2' => 'val2',
'key3' => $data['key2'] //the point
);
?>
I know it will get an error because I called an element whereas an array has not declared yet. But is it possible to do that? The fact, value for 'key2' is dynamically.
No, but you can easily do this:
$data = [
"key1"=>"val1",
"key2"=>"val2"
];
$data["key3"] = $data["key2"];
Or even $data["key3"] = &$data["key2"]; to link them by reference.
PHP processes that in this order:
Create the array with its initialized values.
Assign the array to variable $data.
So no, you can't do it in one line. Of course, you can assign 'val2' to a variable, then assign to both keys.
I have an array like this:
$a = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => array(
'key4' => 'value4',
'key5' => array(
'key6' => 'value6'
)
)
);
as you can see there are inner arrays inside $a
Now, I have a list of keys, example:
key1
key4
key6
I need a script that search if those key exists, and if exists change their values.
I Need to change their values with base64_encode($value_of_the_key)
so Maybe a callback that get the current value and convert it using base64_encode() function.
COuld someone help me?
I'm tring to see the current php functions but it seems there is not ones that do this thing.
THanks
EDIT:
Using the follow code i can get the keys in the callback....but the problem is:
How can i modify the values directly in the array? I Mean.... ok ... i get key and value, but how to change the value in the original array? ($a)
$a = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => array(
'key4' => 'value4',
'key5' => array(
'key6' => 'value6'
)
)
);
function test($item, $key)
{
echo "$key. $item<br />\n";
}
array_walk_recursive($a, 'test');
array_walk_recursive() with callback supplied should help. More info here.
Most of us know the following syntax:
function funcName($param='value'){
echo $param;
}
funcName();
Result: "value"
We were wondering how to pass default values for the 'not last' paramater? I know this terminology is way off, but a simple example would be:
function funcName($param1='value1',$param2='value2'){
echo $param1."\n";
echo $param2."\n";
}
How do we accomplsh the following:
funcName(---default value of param1---,'non default');
Result:
value1
not default
Hope this makes sense, we want to basically assume default values for the paramaters which are not last.
Thanks.
PHP doesn't support what you're trying to do. The usual solution to this problem is to pass an array of arguments:
function funcName($params = array())
{
$defaults = array( // the defaults will be overidden if set in $params
'value1' => '1',
'value2' => '2',
);
$params = array_merge($defaults, $params);
echo $params['value1'] . ', ' . $params['value2'];
}
Example Usage:
funcName(array('value1' => 'one')); // outputs: one, 2
funcName(array('value2' => 'two')); // outputs: 1, two
funcName(array('value1' => '1st', 'value2' => '2nd')); // outputs: 1st, 2nd
funcName(); // outputs: 1, 2
Using this, all arguments are optional. By passing an array of arguments, anything that is in the array will override the defaults. This is possible through the use of array_merge() which merges two arrays, overriding the first array with any duplicate elements in the second array.
Unfortunately, this is not possible.
To get around this, I would suggest adding the following line to your function:
$param1 = (is_null ($param1) ? 'value1' : $param1);
You can then call it like this:
funcName (null, 'non default');
Result:
value1
non default
PHP 8.0 update
Solution you want is available since PHP 8.0: named arguments
function funcName($param1 = 'value1', $param2 = 'value2', $param3 = 'value3') {
...
}
Previously you had to pass some value as $param1, either null or default value, but now you can only pass a second parameter.
funcName(param2: 'value');
And you don't need to care about argument order.
funcName(param2: 'value', param3: 'value');
//is the same as
funcName(param3: 'value', param2: 'value');
Moreover there are some fancy things we can do with named arguments, like passing an array as arguments. It's helpful when we don't know which exact keys we store in an array and we don't need to worry about the order of variables anymore.
$args = [
'param3' => 'value',
'param2' => 'value',
];
funcName(...$args);
//works the same as
funcName(param2: 'value', param3: 'value');
We even don't need to name our values in an array as arguments (of course until we match order of arguments).
$args = [
'value1',
'param3' => 'value3',
'param2' => 'value2',
];
funcName(...$args);
//works the same as
funcName(param1: 'value1', param3: 'value3', param2: 'value2');
//and
funcName('value1', 'value2', 'value3');
The Simple solution of your problem is, instead of using:
funcName(---default value of param1---,'non default');
Use the following Syntax:
funcName('non default',---default value of param1---);
Example:
function setHeight2($maxheight,$minheight = 50) {
echo "The MAX height is : $maxheight <br>";
echo "The MIN height is : $minheight <br>";
}
setHeight2(350,250);
setHeight2(350); // will use the default value of 50
You can use '_' to reference the undefined constant and continue passing variables after that.
For example (expanding from your example):
function funcName($param1='value1',$param2='value2'){
echo $param1."\n";
echo $param2."\n";
}
Calling the function like this:
funcName('_','Some Pass value');
Would output this:
value1
Some Pass value
I have a weird problem here. I'm using an associative array in php (using cakePHP) which has the following form:
$my_array = array(
'data['a']['b'] => 'value1',
'data['b']['c'] => 'value2',
'data['b']['d'] => 'value3',
'data['e'] => array(
'data['e1']['e2']' => 'value3',
'data['e1']['e3']' => 'value4'));
The problem I'm having is that
'data['e1']['e2']' => 'value3' and 'data['e1']['e3']' => 'value4'
are taken like an array like this:
'data['e1']' => array(
['e2'] => 'value3',
['e3'] => 'value4');
I don't want these to be taken as arrays, I want them to be taken as key and value of the array 'data['e']'. As a matter of fact, I want all the elements of the arrays $my_array and 'data['e']' to be taken as keys and values of the corresponding array (not as arrays).
Any help please?
P.S This seems to happen only when I do a debug on cakePHP, if I don't use cakePHP everything seems to be fine and "data" comes from a cURL posted data to cakePHP
Your code is invalid PHP. My best guess is that it should look like this:
$my_array = array(
$data['a']['b'] => 'value1',
$data['b']['c'] => 'value2',
$data['b']['d'] => 'value3',
$data['e'] => array(
$data['e1']['e2'] => 'value3',
$data['e1']['e3'] => 'value4'));
Please show us the contents (for instance, using print_r) of $data.
POSTed data in a certain syntax is automatically parsed into $_POST as array. If you want to get the raw input, use file_get_contents('php://input'). See http://php.net/manual/en/wrappers.php.php.