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.
Related
I wonder if I can select any key inside my array, and set it as the value of another key. in order to be more clear (because my question may not clear enough), I try to do something like this:
$variable = array(
'key' => 'value',
'key2' => $variable['key']
);
As you can see, it won't work (unless I do something like: $variable['key2'] = $variable['key'] out of the array, but this is not what i'm looking for and i'll use it only if I won't be able at all to do it inside the same array).
I searched for any solution but still haven't found one...
There is any way to do such thing inside the same array?
Thank you very much
This way you can`t do it, because this key does not exist yet.
Why to store two same variables in same array? Maybe show us what you are trying to do in bigger picture, so we can help you some way.
Think of it as the code doing what is in the parenthesis FIRST. Since $variable isn't set yet, you'll get an error on $variable['key'], since $variable isn't an array yet.
You must set $variable before
See
$variable = new array();
$variable['key'] = $variable->key2 = 'value';
Also
//create array
$variable = array(
'key' => 'value'
);
//then override
$variable = array(
'key2' => $variable['key'],
'key' => 'new_value'
);
I'm looking for some help finding the best solution to the following problem.
I have several php arrays in their own files looking like this:
<?php
$language_array = Array(
'key'=>'value',
'key'=>'value'
);
?>
What I'm trying to do is get a list of duplicate keys which exists in all the php array files.
I though about checking the first file and first key, and check this against all the other files. Then jump to the next key and continue.
So that I might end up with a list like this:
key = 'key' exist in all files
But I'm not sure about the performance, if I have to open each file x-times I have keys in it.
Any help much appreciated.
EDIT:
I solved it now using a temp array, and then comparing the current key with the temp, and if i have seen it already, add it to an array with just the duplicates.
Performance wise I'm not sure if this is the best solution, but using array_intersect was not working for me as I have no master array with all key=>values
$aOne = Array(
'key1' =>'value1',
'key2' =>'value2',
'key3' =>'value3',
'keyunique1' =>'uniquevalue1'
);
$aTwo = Array(
'key2' =>'value2',
'key3' =>'value3',
'keyunique2' =>'uniquevalue2'
);
$aThree = Array(
'key1' =>'value1',
'key2' =>'value2',
'key3' =>'value3',
'keyunique3' =>'uniquevalue3'
);
$aFour = Array(
'key2' =>'value2',
'keyunique4' =>'uniquevalue4'
);
// Expected result key2 being the only one across all arrays.
$aUniques = array_intersect_key( $aOne, $aTwo, $aThree, $aFour );
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
I want to save current path information in an Array and one field is a part of another. Can I access a field of the same array during initialization?
$this->path = array
(
'rel_image' => '/images',
'document_path' => '/a/file/path',
'path' => $this->path['document_path'].$this->path['rel_images']
);
or do I have to initial them one by one?
The array still is undefined while you're defining it. However you can define other (temporary) variables to do so on the fly:
$this->path = array
(
'rel_image' => $r = '/images',
'document_path' => $p = '/a/file/path',
'path' => $p.$r
);
However that normally should not be needed, as you're duplicating data within the array. Just saying, you can do whatever you want :)
You have to initialize them one by one.
It is best to think of array as a constructor. The array itself doesn't completely exist until after the function call is complete, and you can't access something which doesn't completely exist in most circumstances.
yes, you have to initialize one by one, beacuse $this->path is being filled after array() function is done.
As far as I know, the assignment you're trying to do isn't a functional one.
Code:
<?php $array = array('foo' => 'bar', 'bar' => $array['foo']); ?>
<pre><?php print_r($array); ?></pre>
...renders the following:
Array
(
[foo] => bar
[bar] =>
)
As the array is created at one time, not once per element, it will not be able to reference the values in the same statement as the assignment.
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?