Splitting into a multi-dimensional array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PWGSC/TPSGC

    Splitting into a multi-dimensional array

    I am trying to split a sting into a multi-dimensional array. ex.

    value = "lang~1#name~bo b#age~37#";

    And I want to be able sperate the date to look like

    lang = 1
    name = bob
    age = 37

    I was think about having an array created that would be:

    value[0][0] = lang
    value[0][1] = 1

    value[1][0] = name
    value[1][1] = bob


    value[2][0] = age
    value[2][1] = 37

    But an open to ideas, drawing a little bit of a mind blank...


  • Thomas 'PointedEars' Lahn

    #2
    Re: Splitting into a multi-dimensional array

    PWGSC/TPSGC wrote:
    ^^^^^^^^^^^
    A quite unusual name.
    [color=blue]
    > I am trying to split a sting into a multi-dimensional array. ex.
    >
    > value = "lang~1#name~bo b#age~37#";
    > [...]
    > I was think about having an array created that would be:
    >
    > value[0][0] = lang
    > value[0][1] = 1
    >
    > value[1][0] = name
    > value[1][1] = bob
    >
    > value[2][0] = age
    > value[2][1] = 37
    >
    > But an open to ideas, drawing a little bit of a mind blank...[/color]

    Quickhack:

    value = value.split("#" );
    for (var i = 0; i < value.length; i++)
    {
    var tmp = value[i].split("~");
    value[i] = new Array();
    for (var j = 0; j < tmp.length; j++)
    value[i][j] = tmp[j];
    }

    But a simple object seems to be better than an Array object:

    value = value.split("#" );
    for (var i = 0; i < value.length; i++)
    {
    var tmp = value[i].split("~");
    value[tmp[0]] = tmp[1];
    }


    PointedEars

    Comment

    Working...