Unserialize a single array element in a constant?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • richbl@gmail.com

    #1

    Unserialize a single array element in a constant?

    Hello all,

    I have a question about unserializing a single array element from a
    serialized array. Can this be done, or must I first unserialize the
    array, and then access the element?

    For example, given:

    $data = serialize(array ('4.50','0.00', '0.00'));

    Can I unserialize individual elements from $data, such as:

    $data_element = unserialize($da ta[2]);

    As it is now, PHP will generate a syntax error, forcing me instead to
    unserialize the entire array before accessing it:

    $new_data = unserialize($da ta);
    $data_element = $new_data[2];

    NOTE the value in this is as it relates to CONSTANTS, where arrays must
    be serialized before define()'d as a constant.

    thanks,

    rich

  • Brion Vibber

    #2
    Re: Unserialize a single array element in a constant?

    richbl@gmail.co m wrote:[color=blue]
    > I have a question about unserializing a single array element from a
    > serialized array. Can this be done, or must I first unserialize the
    > array, and then access the element?[/color]

    You either need to unserialize the array first, or write your own
    unserializer which skips the parts of the string you're not interested in.
    [color=blue]
    > Can I unserialize individual elements from $data, such as:
    >
    > $data_element = unserialize($da ta[2]);[/color]

    $data is a string. $data[2] is the third byte in the string. For obvious
    reasons, you can't unserialize much of anything out of that single byte.
    [color=blue]
    > NOTE the value in this is as it relates to CONSTANTS, where arrays must
    > be serialized before define()'d as a constant.[/color]

    Is there any particular reason you're doing that?

    -- brion vibber (brion @ pobox.com)

    Comment

    • richbl@gmail.com

      #3
      Re: Unserialize a single array element in a constant?

      Thanks for the response Brion.
      [color=blue]
      >From reading the documentation on php.net for the define() function,[/color]
      there is a comment made in User Contributed Notes which states:

      22-Sep-2003 06:13
      Constants MUST evaluate to scalar values only.
      You are encouraged to use serialize/unserlialize
      to store/retrieve an array in a constant:

      define('CONST', serialize(array ('a','b','foo'= >'bar')));
      var_dump(CONST) ;
      var_dump(unseri alize(CONST));

      While clearly, any user contribution should be taken with a grain of
      salt, are you suggesting that it is not necessary to first serialize an
      array before making it a constant?

      Let me generalize my original post to a single question:

      How do I create a constant array?

      And then, by extension, how do I access a single constant array
      element?

      I had presumed that an array must first be serialized. Perhaps that's a
      bad assumption.

      thanks,

      rich

      Comment

      • Peter Albertsson

        #4
        Re: Unserialize a single array element in a constant?

        > How do I create a constant array?

        You can't as of my knowledge, and neither can you access elements in the
        array directly without either unserializing it or parsing the serialized
        data.

        Maybe you want to overlook your need of constant arrays?

        Constants as you find in Java and C++ is simply not supported in PHP.

        Regards,

        Peter



        <richbl@gmail.c om> wrote in message
        news:1111170071 .409205.13650@z 14g2000cwz.goog legroups.com...[color=blue]
        > Thanks for the response Brion.
        >[color=green]
        >>From reading the documentation on php.net for the define() function,[/color]
        > there is a comment made in User Contributed Notes which states:
        >
        > 22-Sep-2003 06:13
        > Constants MUST evaluate to scalar values only.
        > You are encouraged to use serialize/unserlialize
        > to store/retrieve an array in a constant:
        >
        > define('CONST', serialize(array ('a','b','foo'= >'bar')));
        > var_dump(CONST) ;
        > var_dump(unseri alize(CONST));
        >
        > While clearly, any user contribution should be taken with a grain of
        > salt, are you suggesting that it is not necessary to first serialize an
        > array before making it a constant?
        >
        > Let me generalize my original post to a single question:
        >
        > How do I create a constant array?
        >
        > And then, by extension, how do I access a single constant array
        > element?
        >
        > I had presumed that an array must first be serialized. Perhaps that's a
        > bad assumption.
        >
        > thanks,
        >
        > rich
        >[/color]


        Comment

        • Brion Vibber

          #5
          Re: Unserialize a single array element in a constant?

          richbl@gmail.co m wrote:[color=blue]
          > 22-Sep-2003 06:13
          > Constants MUST evaluate to scalar values only.
          > You are encouraged to use serialize/unserlialize
          > to store/retrieve an array in a constant:[/color]
          [snip][color=blue]
          > While clearly, any user contribution should be taken with a grain of
          > salt, are you suggesting that it is not necessary to first serialize an
          > array before making it a constant?[/color]

          No, I'm wondering why you're trying to use a constant for an array, when
          you already know that constants can't contain arrays. I'm wondering if
          there's some specific reason that you MUST use a constant for this (in
          which case a workaround such as serialization/unserialization or
          implode/explode would be necessary to get an array value out of a
          string) or if you're simply making your task more difficult by picking
          an unsuitable tool without a good reason for it.
          [color=blue]
          > Let me generalize my original post to a single question:
          >
          > How do I create a constant array?[/color]

          You can't.

          See: http://www.php.net/manual/en/language.constants.php
          [color=blue]
          > And then, by extension, how do I access a single constant array
          > element?[/color]

          Constants cannot have array values, so there is no such thing as a
          constant array element.

          Use a variable or some other way of storing your data as an array if you
          need to be able to access elements of an array value directly.

          -- brion vibber (brion @ pobox.com)

          Comment

          • richbl@gmail.com

            #6
            Re: Unserialize a single array element in a constant?

            Thanks Peter, that confirms my recent experience.

            I can certainly get by using a standard array, but--as you guessed--I'm
            coming from the C/C++ world, and was looking for an equivalent analog.

            rich

            Comment

            • richbl@gmail.com

              #7
              Re: Unserialize a single array element in a constant?

              Thanks for the clarification, Brion.

              However, constants can contain arrays, BUT they must first be
              serialized, and... where I had my particular question, it is the case
              that they must then be unserialized first before accessing them. Though
              I suppose, technically, if an array is first serialized before
              define()'d, one could argue that what's actually contained in the
              constant is a string (a scalar which CAN be stored as a constant).

              In any case, it would be very hard to defend the act of creating a
              constant array from any kind of performance metric: I'm quite certain
              that the overhead of first serializing and then unserializing such a
              structure is, while not prohibitive, expensive.

              I have my answers.

              thanks again,

              rich

              Comment

              • Chung Leong

                #8
                Re: Unserialize a single array element in a constant?

                <richbl@gmail.c om> wrote in message
                news:1111170071 .409205.13650@z 14g2000cwz.goog legroups.com...[color=blue]
                > Thanks for the response Brion.
                >[color=green]
                > >From reading the documentation on php.net for the define() function,[/color]
                > there is a comment made in User Contributed Notes which states:
                >
                > 22-Sep-2003 06:13
                > Constants MUST evaluate to scalar values only.
                > You are encouraged to use serialize/unserlialize
                > to store/retrieve an array in a constant:
                >
                > define('CONST', serialize(array ('a','b','foo'= >'bar')));
                > var_dump(CONST) ;
                > var_dump(unseri alize(CONST));
                >
                > While clearly, any user contribution should be taken with a grain of
                > salt, are you suggesting that it is not necessary to first serialize an
                > array before making it a constant?[/color]

                Hmmmm. A pretty lame way to get around a limitation. Do you absolutely have
                to use a constant? Why not just define a function?


                Comment

                Working...