|
Posted by Hilarion on 10/10/45 11:24
Dave Thomas wrote:
>I have:
> $this_array = array($test_year, $test_color, $test_food)
>
> I want to access another variable based on $test_year, only it will be
> called $test_month (test could be something else). $test_year is a form
> and I want to access $test_month.
>
> If I use $this_array[1] then I am getting the actual value of the form.
> I want to get the name of the variable I am accessing!
In this case it's not possible. Your array $this_array does not
store variable names.
This line of code:
$this_array = array($test_year, $test_color, $test_food)
creates array with value of $test_year being first element (index 0),
value of $test_color being second etc. As you can see only values
of those variables are stored in the array. If you are building
this array, then you could store the name too in many different
ways. Here two examples:
- As key:
$this_array = array(
'test_year' => $test_year,
'test_color' => $test_color,
'test_food' => $test_food
)
but in this case you'll have to use those names as indexes
($this_array['test_color'] instead of $this_array[1]) or
use array traversal functions.
- With subarrays:
$this_array = array(
array( 'test_year', $test_year ),
array( 'test_color', $test_color ),
array( 'test_food', $test_food )
)
but in this case you'll have to use two indexes:
$this_array[0][1] to access $test_year value and $this_array[0][0]
to get 'test_year'.
- You could also mix above methods or use objects etc.
Which method is best? That depends on what you are trying to do...
Hilarion
Navigation:
[Reply to this message]
|