Recreating array from SQL field

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • José Luis Ramírez

    Recreating array from SQL field

    Hi,

    I'm trying to recreate an array from a var_export()'ed array stored in
    a database, using eval().

    Basically I have something this in my database:

    array (
    0 => 1,
    1 => 2,
    2 => array (
    0 => 'a',
    1 => 'b',
    2 => 'c',
    ),
    )


    If I then read it into a variable:

    $stored_array = /*whatever_sql_s tatements_for_r eading_stored_a rray*/

    I would think that something like:

    eval('$recreate d_array = $store_array');

    would do, but it sjust doesn't work, and I'm quite stumped with this
    simple task, but I'd appreciate anyone' guidance.

    Cheers!

    - José

    p.s. The solution would be be a welcome addition to the PHP
    documentation since there is only a hint of how one could do this but
    not a working example.

  • Michael Fesser

    #2
    Re: Recreating array from SQL field

    .oO(José Luis Ramírez)
    [color=blue]
    >I'm trying to recreate an array from a var_export()'ed array stored in
    >a database, using eval().[/color]

    eval() is slow. What about serialize()/unserialize()?
    [color=blue]
    >I would think that something like:
    >
    >eval('$recreat ed_array = $store_array');
    >
    >would do, but it sjust doesn't work, and I'm quite stumped with this
    >simple task, but I'd appreciate anyone' guidance.[/color]

    eval("\$recreat ed_array = $store_array;") ;
    [color=blue]
    >p.s. The solution would be be a welcome addition to the PHP
    >documentatio n since there is only a hint of how one could do this but
    >not a working example.[/color]

    There are working examples for eval() in the manual.

    Micha

    Comment

    • José Luis Ramírez

      #3
      Re: Recreating array from SQL field

      Michael Fesser wrote:[color=blue]
      > .oO(José Luis Ramírez)
      >[color=green]
      > >I'm trying to recreate an array from a var_export()'ed array stored[/color][/color]
      in[color=blue][color=green]
      > >a database, using eval().[/color]
      >
      > eval() is slow. What about serialize()/unserialize()?
      >[/color]

      Yes. I had already done some tests with serialize()/unserialize(). But
      was derailed after finding var_export().

      I'm going back to serialize()/unserialize(). Thanks.

      [color=blue][color=green]
      > >I would think that something like:
      > >
      > >eval('$recreat ed_array = $store_array');
      > >
      > >would do, but it sjust doesn't work, and I'm quite stumped with this
      > >simple task, but I'd appreciate anyone' guidance.[/color]
      >
      > eval("\$recreat ed_array = $store_array;") ;[/color]



      So the only difference was the missing semi-colon? I'm testing it right
      now.

      [color=blue][color=green]
      > >p.s. The solution would be be a welcome addition to the PHP
      > >documentatio n since there is only a hint of how one could do this[/color][/color]
      but[color=blue][color=green]
      > >not a working example.[/color]
      >
      > There are working examples for eval() in the manual.
      >
      > Micha[/color]

      I suppose I didn't make myself clear. There ARE a lot of examples on
      the eval() page, but not any that tell you explicitly how to recreate a
      var_export()'ed array, at least since I checked yesterday, I could be
      wrong, maybe I missed the example. Anyway, thanks!

      J.L.

      Comment

      • Michael Fesser

        #4
        Re: Recreating array from SQL field

        .oO(José Luis Ramírez)
        [color=blue]
        >Michael Fesser wrote:
        >[color=green]
        >> eval("\$recreat ed_array = $store_array;") ;[/color]
        >
        >So the only difference was the missing semi-colon?[/color]

        Not exactly. Compare the two:

        eval('$recreate d_array = $store_array');
        eval("\$recreat ed_array = $store_array;") ;

        Differences in the second:

        * double quotes around the string, so embedded variables will be
        replaced by their values
        * an escaped $ on the first variable, this means it will be passed as-is
        to eval(), only $store_array will be replaced by its value
        * the missing semicolon

        This is the string eval() will finally get and execute:

        $recreated_arra y = array(... stuff from db here ...);
        [color=blue]
        >I suppose I didn't make myself clear. There ARE a lot of examples on
        >the eval() page, but not any that tell you explicitly how to recreate a
        >var_export()'e d array, at least since I checked yesterday, I could be
        >wrong, maybe I missed the example. Anyway, thanks![/color]

        There is an (bad, but working) example of how to assign a value to a
        variable with eval(). It doesn't matter if it's a scalar or an array,
        the syntax is the same. You only have to know when it's necessary to
        escape a $ and when not.

        Micha

        Comment

        Working...