#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Using the value of an array element as the position marker of another array.


    Hi,

    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[0] contains "3".

    I'd like to use the "3" as a position marker for a second array, as in...

    $fieldsB[3] = "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. ;-)

    Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    828
    Rep Power
    496
    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.

IMN logo majestic logo threadwatch logo seochat tools logo