Web Analytics Made Easy -
StatCounter Values on both sides of a predefined character - CodingForum

Announcement

Collapse
No announcement yet.

Values on both sides of a predefined character

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Values on both sides of a predefined character

    I have a database field with this in it:"3:1,4:2,1:3" without the quotes.
    I retrieved the field and exploded the commas, so now I have an array with the values:
    PHP Code:
    [0] => 3:[1] => 4:[2] => 1:
    Now what I want to do, is basically 'explode' the ':' and put each number on either side of the colon into two separate variables, for every element in the array, but I can't figure out how to do it.

    Basically what I want is $var1=3, $var2=1, $var3=4, etc..

    Any suggestions/solutions?

    Thanks

  • #2
    PHP Code:
    $var "3:1,4:2,1:3"// get string from db
    $arr preg_split('/[\:\,]+/'$var); // split string with regexp rule and get array( [0] => 3, [1] => 1, [2] => 4, [3] => 2 ...)
    for($i 0$i count($arr); $i++)
    {
        
    $var_name 'var' . ($i 1); // assign a name to variable 
        
    $$var_name $arr[$i]; // assign a value to variable
    }

    /* checking with output */
    for($i 1$i <= 6$i++)
    {
        
    $var_name 'var' $i;
        echo 
    $var_name ' = ' . $$var_name "\r\n";

    Last edited by poyzn; Aug 18, 2011, 03:20 AM.
    custom web development rubyroid.com

    Comment


    • #3
      Originally posted by poyzn View Post
      PHP Code:
      $var "3:1,4:2,1:3"// get string from db
      $arr preg_split('/[\:\,]+/'$var); // split string with regexp rule and get array( [0] => 3, [1] => 1, [2] => 4, [3] => 2 ...)
      for($i 0$i count($arr); $i++)
      {
          
      $var_name 'var' . ($i 1); // assign a name to variable 
          
      $$var_name $arr[$i]; // assign a value to variable
      }

      /* checking with output */
      for($i 1$i <= 6$i++)
      {
          
      $var_name 'var' $i;
          echo 
      $var_name ' = ' . $$var_name "\r\n";

      If you don't mind me saying so, I don't think that's an efficient way of getting the result. Plus, in your second loop you're only gonna loop 6 values ever.

      Also, I don't see the value of separating them into individual variables. This is how I would achieve a similar result:
      PHP Code:
      $var = array();
      $raw = array();
      $raw2 = array();
      while(
      $row mysql_fetch_array($result)){
          
      // Populate the raw array with all the formatted information from every row
          
      $raw explode(','$row['field_in_question']);
          foreach(
      $raw as $value){
              
      $raw2 explode(':'$value);
              
      $var[$raw2[0]] = $raw2[1];
          }

      This way, $var[left of :] = right of :. You can run a foreach on this loop to get every value easily, making it completely dynamic. Be careful with this though - left of : will have to be unique.

      Also, this method doesn't require the regex engine, making it a bit faster I think. Obviously for large amounts of information regex will overtake it, but this method should be fine and faster for now.

      In your method, you can only refer to the variables by using another variable, which makes it quite impractical. By using an array like this, you can output the result simply like this:
      PHP Code:
      foreach($var as $key => $value){
          echo 
      'var '.$key.' has value '.$value.'\n';

      Untested, but should work.

      Edit: Optimised further.
      Last edited by BluePanther; Aug 18, 2011, 07:37 PM.
      Useful function to retrieve difference in times
      The best PHP resource
      A good PHP FAQ
      PLEASE remember to wrap your code in [PHP] tags.
      PHP Code:
      // Replace this
      if(isset($_POST['submitButton']))
      // With this
      if(!empty($_POST))
      // Then check for values/forms. Some IE versions don't send the submit button 
      Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

      Comment


      • #4
        I posted the edit later on, and dont think it alerts peoploe after edit. So posted in an attempt to alert to the edit
        Useful function to retrieve difference in times
        The best PHP resource
        A good PHP FAQ
        PLEASE remember to wrap your code in [PHP] tags.
        PHP Code:
        // Replace this
        if(isset($_POST['submitButton']))
        // With this
        if(!empty($_POST))
        // Then check for values/forms. Some IE versions don't send the submit button 
        Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

        Comment


        • #5
          Originally posted by BluePanther View Post
          I posted the edit later on, and dont think it alerts peoploe after edit. So posted in an attempt to alert to the edit
          Check what Infect wants:

          Originally posted by Infect
          Basically what I want is $var1=3, $var2=1, $var3=4, etc..
          1. He'll receive an array with wrong values with your script.

          2. Part for checking with output is for just checking cause I've tested the script.

          2. Agree that explode() will be faster
          custom web development rubyroid.com

          Comment


          • #6
            Originally posted by poyzn View Post
            Check what Infect wants:



            1. He'll receive an array with wrong values with your script.

            2. Part for checking with output is for just checking cause I've tested the script.

            2. Agree that explode() will be faster
            I know that $var1, $var2 etc is what he asked for, but how will he reference a theoretical infinite amount with no way of knowing how many var's there are? It's not a practical way of achieving his result so I suggested a better method for him.

            There is nothing wrong with my value assignment. Just tested it - works as expected. The 'output checking' is exactly my point with assigning an incrementing variable instead of an associative array - you used $i<6 because you needed to know how many of these variables there are. With an array, you don't need to do that. count() will give the number of values, if you so wish, with count()-1 giving the top index of the array. A foreach loop will give a complete output of the array. He could use array_key_exists($var['number left of :']) to check for a value in his array.

            In other words, it is possible to have an incrementing variable, but the impracticalities make the feature useless imo. An associative array will give an easier to use result.
            Useful function to retrieve difference in times
            The best PHP resource
            A good PHP FAQ
            PLEASE remember to wrap your code in [PHP] tags.
            PHP Code:
            // Replace this
            if(isset($_POST['submitButton']))
            // With this
            if(!empty($_POST))
            // Then check for values/forms. Some IE versions don't send the submit button 
            Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

            Comment


            • #7
              Here you go

              Code:
              $data = '3:1,4:2,1:3';
              
              $data = explode(',', $data);
              
              $i = 0;
              
              foreach ($data as $d) {
              
              	$name1 = 'var'.++$i;
              	$name2 = 'var'.++$i;
              
              	list($$name1, $$name2) = explode(':', $d);
              }
              
              echo ("\$var1 = $var1<br />\$var2 = $var2<br />\$var3 = $var3<br />\$var4 = $var4<br />\$var5 = $var5<br />\$var6 = $var6<br />");

              Output

              Code:
              $var1 = 3
              $var2 = 1
              $var3 = 4
              $var4 = 2
              $var5 = 1
              $var6 = 3

              Comment

              Working...
              X
              😀
              🥰
              🤢
              😎
              😡
              👍
              👎