Reputation: 3591
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?
Upvotes: 1
Views: 87
Reputation: 197599
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 :)
Upvotes: 2
Reputation: 6071
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.
Upvotes: 0
Reputation: 81988
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.
Upvotes: 1
Reputation: 50966
yes, you have to initialize one by one, beacuse $this->path is being filled after array() function is done.
Upvotes: 0