August 19th, 2013, 10:30 AM
Using the value of an array element as the position marker of another array.
I'm wondering how I can use the numeric value of one array element, as an position marker for a second array.
For example... I have
$fieldsA contains "3".
I'd like to use the "3" as a position marker for a second array, as in...
$fieldsB = "Some other value"
To be even more accurate in my array positions, I think I'd need to subtract 1 from the array element's position, as in...
$fieldsB[3 -1] = "Some other value"
Now I know this most likely isn't the right code, but that's the reason I'm asking the experts. ;-)
August 19th, 2013, 01:00 PM
Sure you can do that. Sometime, this is exactly what you need, using the values of an array as subscriupts of another array. Or the value of a hash as a key to another hash. Nothing wrong with these ideas, per se.
But this looks as a XY problem: you need to solve the X problem, you think that you could do it using Y, and ask is there is any better way than Y. But you did not tel what X is.
Your construction may be perfectly legitimate, or may be it would be much better to do something else (possibly a nested data structure such an array of arrays would be better, or maybe not). But we cannot say so long as you don't say what you are really trying to achieve.