converting array $row values to $string

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

    converting array $row values to $string

    hi all

    is there a function to convert all values of array $row into seperate
    variables:

    $row = mysql_fetch_arr ay($result);

    // i dont want to have to do this anymore :-)

    $name = $row['name'];
    $address1 = $row['address1'];
    $address2 = $row['address2'];
    etc etc etc




    Regards

    marc

  • Nyoka

    #2
    Re: converting array $row values to $string

    You could write your own function similar to:
    function convertArray ($array) {
    foreach ($array AS $name =$value) {
    global $$name;
    $$name = $value;
    }
    }

    Make sure you do a print_r ($array) before using this so you are away
    of what variables are going to be used.

    monomaniac21 wrote:
    hi all
    >
    is there a function to convert all values of array $row into seperate
    variables:
    >
    $row = mysql_fetch_arr ay($result);
    >
    // i dont want to have to do this anymore :-)
    >
    $name = $row['name'];
    $address1 = $row['address1'];
    $address2 = $row['address2'];
    etc etc etc
    >
    >
    >
    >
    Regards
    >
    marc

    Comment

    • Hans 'pritaeas' Pollaerts

      #3
      Re: converting array $row values to $string

      Maybe this works (untested):

      $row = mysql_fetch_arr ay($result);
      foreach ($row as $name =$value) {
      $eval = "${$name} = $value";
      eval ($eval);
      }

      "monomaniac 21" <mcyi2mr3@googl email.comwrote in message
      news:1153390678 .605323.145130@ h48g2000cwc.goo glegroups.com.. .
      hi all
      >
      is there a function to convert all values of array $row into seperate
      variables:
      >
      $row = mysql_fetch_arr ay($result);
      >
      // i dont want to have to do this anymore :-)
      >
      $name = $row['name'];
      $address1 = $row['address1'];
      $address2 = $row['address2'];
      etc etc etc
      >
      >
      >
      >
      Regards
      >
      marc
      >

      Comment

      • monomaniac21

        #4
        Re: converting array $row values to $string

        Cheers Nyoka I'm sure it does work.

        but i dont understand it. what is the double $$ for and "global"?

        :-)


        Nyoka wrote:
        You could write your own function similar to:
        function convertArray ($array) {
        foreach ($array AS $name =$value) {
        global $$name;
        $$name = $value;
        }
        }
        >
        Make sure you do a print_r ($array) before using this so you are away
        of what variables are going to be used.
        >
        monomaniac21 wrote:
        hi all

        is there a function to convert all values of array $row into seperate
        variables:

        $row = mysql_fetch_arr ay($result);

        // i dont want to have to do this anymore :-)

        $name = $row['name'];
        $address1 = $row['address1'];
        $address2 = $row['address2'];
        etc etc etc




        Regards

        marc

        Comment

        • monomaniac21

          #5
          Re: converting array $row values to $string

          What i cant see is how the function finds out the name given to each
          row of the array?

          Can anyone explain this to me?

          monomaniac21 wrote:
          Cheers Nyoka I'm sure it does work.
          >
          but i dont understand it. what is the double $$ for and "global"?
          >
          :-)
          >
          >
          Nyoka wrote:
          You could write your own function similar to:
          function convertArray ($array) {
          foreach ($array AS $name =$value) {
          global $$name;
          $$name = $value;
          }
          }

          Make sure you do a print_r ($array) before using this so you are away
          of what variables are going to be used.

          monomaniac21 wrote:
          hi all
          >
          is there a function to convert all values of array $row into seperate
          variables:
          >
          $row = mysql_fetch_arr ay($result);
          >
          // i dont want to have to do this anymore :-)
          >
          $name = $row['name'];
          $address1 = $row['address1'];
          $address2 = $row['address2'];
          etc etc etc
          >
          >
          >
          >
          Regards
          >
          marc

          Comment

          • ImOk

            #6
            Re: converting array $row values to $string

            After you do the fetch run

            extract($row);

            This will cretae memory variables of the same name as the keys and
            containing the values.

            Study the example from docs:

            $size = "large";
            $var_array = array("color" ="blue",
            "size" ="medium",
            "shape" ="sphere");
            extract($var_ar ray, EXTR_PREFIX_SAM E, "wddx");

            echo "$color, $size, $shape, $wddx_size\n";




            monomaniac21 wrote:
            hi all
            >
            is there a function to convert all values of array $row into seperate
            variables:
            >
            $row = mysql_fetch_arr ay($result);
            >
            // i dont want to have to do this anymore :-)
            >
            $name = $row['name'];
            $address1 = $row['address1'];
            $address2 = $row['address2'];
            etc etc etc
            >
            >
            >
            >
            Regards
            >
            marc

            Comment

            • Kimmo Laine

              #7
              Re: converting array $row values to $string

              "monomaniac 21" <mcyi2mr3@googl email.comwrote in message
              news:1153393402 .467503.199960@ i42g2000cwa.goo glegroups.com.. .
              What i cant see is how the function finds out the name given to each
              row of the array?
              >
              Can anyone explain this to me?

              It's using a feature of php called variable variable.

              let's say you have a couple of variables:
              $name = "fred flintstone";
              $pointer = "name";

              You can point to variable $name with the $pointer using the variable
              variable syntax: $$pointer is evaluated as $"name", and since $pointer
              contains the string "name".

              echo $$pointer; // This will print "fred flintstone"

              The very same mechanisms is used in the example function.

              --
              "ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
              spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)


              Comment

              • Kimmo Laine

                #8
                Re: converting array $row values to $string

                "Hans 'pritaeas' Pollaerts" <pritaeas@hotma il.comwrote in message
                news:e9nmo0$6un $1@news3.zwoll1 .ov.home.nl...
                Maybe this works (untested):
                >
                $row = mysql_fetch_arr ay($result);
                foreach ($row as $name =$value) {
                $eval = "${$name} = $value";
                eval ($eval);
                }

                No reason to get eval involved.
                This:

                foreach($row as $name =$value)
                $$name = $value;

                works just fine.


                --
                "ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
                spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)


                Comment

                • Markus Ernst

                  #9
                  Re: converting array $row values to $string

                  monomaniac21 schrieb:
                  Cheers Nyoka I'm sure it does work.
                  >
                  but i dont understand it. what is the double $$ for and "global"?
                  manual_exists() == true!



                  :-)

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: converting array $row values to $string

                    monomaniac21 wrote:
                    hi all
                    >
                    is there a function to convert all values of array $row into seperate
                    variables:
                    >
                    $row = mysql_fetch_arr ay($result);
                    >
                    // i dont want to have to do this anymore :-)
                    >
                    $name = $row['name'];
                    $address1 = $row['address1'];
                    $address2 = $row['address2'];
                    etc etc etc
                    >
                    >
                    >
                    >
                    Regards
                    >
                    marc
                    >
                    Why not just

                    list($name, $address1, $address2) = mysql_fetch_arr ay($result);

                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===

                    Comment

                    • Rik

                      #11
                      Re: converting array $row values to $string

                      Jerry Stuckle wrote:
                      list($name, $address1, $address2) = mysql_fetch_arr ay($result);
                      Better:
                      list($name, $address1, $address2) = mysql_fetch_row ($result);

                      Grtz,
                      --
                      Rik Wasmus


                      Comment

                      • Jerry Stuckle

                        #12
                        Re: converting array $row values to $string

                        Rik wrote:
                        Jerry Stuckle wrote:
                        >
                        > list($name, $address1, $address2) = mysql_fetch_arr ay($result);
                        >
                        >
                        Better:
                        list($name, $address1, $address2) = mysql_fetch_row ($result);
                        >
                        Grtz,
                        True.

                        --
                        =============== ===
                        Remove the "x" from my email address
                        Jerry Stuckle
                        JDS Computer Training Corp.
                        jstucklex@attgl obal.net
                        =============== ===

                        Comment

                        Working...