How to convert array to string, and vice versa

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Streater

    How to convert array to string, and vice versa

    I have some items, numbered from 0 upwards. Some of them may have a
    string attached. All these items need to be represented in a single
    already existing database record. So, I thought of taking an array, as
    it might be looking thus (the values are all strings):

    Key Value
    --- -----
    0 firstone
    2 somestring
    5 anotherstr


    and so on, and converting it to a single string:

    "'0', 'firstone', '2', 'somestring', '5', 'anotherstr'"

    then I have a string I can write to the database record.

    So I want to go from:

    $myarr = array (0 => 'firstone', 2 => 'somestring', 5 => 'anotherstr');

    to:

    $mystring = "'0', 'firstone', '2', 'somestring', '5', 'anotherstr'";

    Is there a quick way to do this mapping in both directions? (I don't
    mind if the string read/written to the database is not as above, as it
    won't be used for any other purpose).

    I can't see any array or string function that looks designed for this or
    a similar purpose.

    Thanks for any pointers.

    -- tim
  • David Haynes

    #2
    Re: How to convert array to string, and vice versa

    Tim Streater wrote:[color=blue]
    > I have some items, numbered from 0 upwards. Some of them may have a
    > string attached. All these items need to be represented in a single
    > already existing database record. So, I thought of taking an array, as
    > it might be looking thus (the values are all strings):
    >
    > Key Value
    > --- -----
    > 0 firstone
    > 2 somestring
    > 5 anotherstr
    >
    >
    > and so on, and converting it to a single string:
    >
    > "'0', 'firstone', '2', 'somestring', '5', 'anotherstr'"
    >
    > then I have a string I can write to the database record.
    >
    > So I want to go from:
    >
    > $myarr = array (0 => 'firstone', 2 => 'somestring', 5 => 'anotherstr');
    >
    > to:
    >
    > $mystring = "'0', 'firstone', '2', 'somestring', '5', 'anotherstr'";
    >
    > Is there a quick way to do this mapping in both directions? (I don't
    > mind if the string read/written to the database is not as above, as it
    > won't be used for any other purpose).
    >
    > I can't see any array or string function that looks designed for this or
    > a similar purpose.
    >
    > Thanks for any pointers.
    >
    > -- tim[/color]

    Something like this could be used to pack the string:

    $foreach( $myarr as $key => $value ) {
    $tmp[] = "'$key'";
    $tmp[] = "'$value'";
    }
    $mystring = '"'.implode( ', ', $tmp).'"';

    Unpacking would be something like:
    $tmp = substr($mystrin g, 1, strlen($mystrin g)-1);
    $myarr = explode(', ', $tmp);

    You may have to fudge it a bit to get the quotes all correct.

    -david-

    Comment

    • Oli Filth

      #3
      Re: How to convert array to string, and vice versa

      Tim Streater said the following on 23/05/2006 17:49:[color=blue]
      > I have some items, numbered from 0 upwards. Some of them may have a
      > string attached. All these items need to be represented in a single
      > already existing database record. So, I thought of taking an array, as
      > it might be looking thus (the values are all strings):
      >
      > Key Value
      > --- -----
      > 0 firstone
      > 2 somestring
      > 5 anotherstr
      >
      >
      > and so on, and converting it to a single string:
      >
      > "'0', 'firstone', '2', 'somestring', '5', 'anotherstr'"
      >
      > then I have a string I can write to the database record.
      >[/color]

      Is it absolutely mandatory that you put these into a single record?
      This is almost certainly a bad idea. Not only do you have to write
      conversion functions to get data into and out of the database, but you
      data is no longer atomic. Amongst other things, this makes it difficult
      to search, index, delete or reference your data...


      --
      Oli

      Comment

      • Tim Streater

        #4
        Re: How to convert array to string, and vice versa

        In article <xvHcg.10455$S. 4851@newsfe3-win.ntli.net>,
        Oli Filth <catch@olifilth .co.uk> wrote:
        [color=blue]
        > Tim Streater said the following on 23/05/2006 17:49:[color=green]
        > > I have some items, numbered from 0 upwards. Some of them may have a
        > > string attached. All these items need to be represented in a single
        > > already existing database record. So, I thought of taking an array, as
        > > it might be looking thus (the values are all strings):
        > >
        > > Key Value
        > > --- -----
        > > 0 firstone
        > > 2 somestring
        > > 5 anotherstr
        > >
        > >
        > > and so on, and converting it to a single string:
        > >
        > > "'0', 'firstone', '2', 'somestring', '5', 'anotherstr'"
        > >
        > > then I have a string I can write to the database record.
        > >[/color]
        >
        > Is it absolutely mandatory that you put these into a single record?
        > This is almost certainly a bad idea. Not only do you have to write
        > conversion functions to get data into and out of the database, but you
        > data is no longer atomic. Amongst other things, this makes it difficult
        > to search, index, delete or reference your data...[/color]

        The one record in question is for an interface card, typically in a
        router or an SDH box. Up to now the ports on the card have been
        considered identical and are numbered incrementally, so I have just kept
        a record for the card, with a field numports. Now we wish to keep
        information about the port optics (short-reach, long-reach, etc). With
        the advent of plug-in optics this may vary from port to port.

        The most general/flexible/futureproof way to do this would evidently be
        to have a set of port-records, pointing to the interface card. Then,
        obviously, the optics information would just be a field in the port
        record.

        I have resisted doing it this way up to now, because:

        1) this would be a major change to the way the data is held, and more
        importantly, displayed on our web pages, and interacted with.

        2) it would prolly slow down the functionality a lot as there would be
        an extra layer of things to do per port.

        3) It's seemed a bit heavy to do this for just one bit of information
        per port

        4) I haven't got time to do this much work on this item.

        So, I am looking for a quick'n'dirty fix :-) which will cause minimal
        disruption on the rest of the structure.

        I agree with your prospective downsides.

        -- tim

        Comment

        • Tim Streater

          #5
          Re: How to convert array to string, and vice versa

          In article <lfHcg.74213$ZF .33611@fe55.use netserver.com>,
          David Haynes <david.haynes2@ sympatico.ca> wrote:
          [color=blue]
          > Tim Streater wrote:[color=green]
          > > I have some items, numbered from 0 upwards. Some of them may have a
          > > string attached. All these items need to be represented in a single
          > > already existing database record. So, I thought of taking an array, as
          > > it might be looking thus (the values are all strings):
          > >
          > > Key Value
          > > --- -----
          > > 0 firstone
          > > 2 somestring
          > > 5 anotherstr
          > >
          > >
          > > and so on, and converting it to a single string:
          > >
          > > "'0', 'firstone', '2', 'somestring', '5', 'anotherstr'"
          > >
          > > then I have a string I can write to the database record.
          > >
          > > So I want to go from:
          > >
          > > $myarr = array (0 => 'firstone', 2 => 'somestring', 5 => 'anotherstr');
          > >
          > > to:
          > >
          > > $mystring = "'0', 'firstone', '2', 'somestring', '5', 'anotherstr'";
          > >
          > > Is there a quick way to do this mapping in both directions? (I don't
          > > mind if the string read/written to the database is not as above, as it
          > > won't be used for any other purpose).
          > >
          > > I can't see any array or string function that looks designed for this or
          > > a similar purpose.
          > >
          > > Thanks for any pointers.
          > >
          > > -- tim[/color]
          >
          > Something like this could be used to pack the string:
          >
          > $foreach( $myarr as $key => $value ) {
          > $tmp[] = "'$key'";
          > $tmp[] = "'$value'";
          > }
          > $mystring = '"'.implode( ', ', $tmp).'"';
          >
          > Unpacking would be something like:
          > $tmp = substr($mystrin g, 1, strlen($mystrin g)-1);
          > $myarr = explode(', ', $tmp);
          >
          > You may have to fudge it a bit to get the quotes all correct.[/color]

          David,

          No time to look at that carefully tonight but I'll check it tomorrow.

          Thanks,

          -- tim

          Comment

          • tim

            #6
            Re: How to convert array to string, and vice versa


            David Haynes wrote:[color=blue]
            > Tim Streater wrote:[color=green]
            > > So I want to go from:
            > >
            > > $myarr = array (0 => 'firstone', 2 => 'somestring', 5 => 'anotherstr');
            > >
            > > to:
            > >
            > > $mystring = "'0', 'firstone', '2', 'somestring', '5', 'anotherstr'";[/color][/color]
            [color=blue]
            >
            > Something like this could be used to pack the string:
            >
            > $foreach( $myarr as $key => $value ) {
            > $tmp[] = "'$key'";
            > $tmp[] = "'$value'";
            > }
            > $mystring = '"'.implode( ', ', $tmp).'"';
            >
            > Unpacking would be something like:
            > $tmp = substr($mystrin g, 1, strlen($mystrin g)-1);
            > $myarr = explode(', ', $tmp);[/color]

            array keys from the original $myarr are now array values in the new
            $myarr
            [color=blue]
            > You may have to fudge it a bit to get the quotes all correct.[/color]

            The quotes are a problem
            [color=blue]
            > -david-[/color]

            What Oli said is right but if you want to do it this way then David's
            suggestion of using a foreach/implode/explode would work.

            You could make the packing and unpacking, especially the unpacking,
            easier and cleaner if you do not quote the keys/values and if you use
            two seperators. For example & and = from url query strings.

            Tim

            Comment

            • Christopher Vogt

              #7
              Re: How to convert array to string, and vice versa

              only read the title not your posting, but you might be searching for

              serialize()
              unserialize()

              Generates a storable representation of a value

              Creates a PHP value from a stored representation

              Comment

              • Tim Streater

                #8
                Re: How to convert array to string, and vice versa

                In article <xvHcg.10455$S. 4851@newsfe3-win.ntli.net>,
                Oli Filth <catch@olifilth .co.uk> wrote:
                [color=blue]
                > Tim Streater said the following on 23/05/2006 17:49:[color=green]
                > > I have some items, numbered from 0 upwards. Some of them may have a
                > > string attached. All these items need to be represented in a single
                > > already existing database record. So, I thought of taking an array, as
                > > it might be looking thus (the values are all strings):
                > >
                > > Key Value
                > > --- -----
                > > 0 firstone
                > > 2 somestring
                > > 5 anotherstr
                > >
                > >
                > > and so on, and converting it to a single string:
                > >
                > > "'0', 'firstone', '2', 'somestring', '5', 'anotherstr'"
                > >
                > > then I have a string I can write to the database record.
                > >[/color]
                >
                > Is it absolutely mandatory that you put these into a single record?
                > This is almost certainly a bad idea. Not only do you have to write
                > conversion functions to get data into and out of the database, but you
                > data is no longer atomic. Amongst other things, this makes it difficult
                > to search, index, delete or reference your data...[/color]

                Oli,

                Thanks for pushing me to re-think my strategy. Choosing a good data
                representation is most of the battle and while driving home and back to
                work this morning [1] I had a think about better ways to represent the
                data which would retain the flexibility while not burdening the general
                and presentation suite. Generally I don't like hacks anyway as they tend
                to come back to bite you.

                Thanks to the other responders - there are many functions in PHP I am
                not familiar with so being pointed at some new ones for a specific
                purpose will be a good exercise.

                [1] Radio 4 only had an unfunny comedy program last night and a poor
                magazine program this morning, as it happened, so I turned the radio off
                while driving.

                Cheers,

                -- tim

                Comment

                Working...