new variable as mix of other variables

Discussion in 'PHP' started by Alex69, Jan 20, 2011.

  1. #1
    i am trying to figure out how to get a new variable as mix of other variables. Below code:

    		$results = array("a","b","c","d");
    		shuffle($results);
    		
    		foreach ($results as $result) {
    			echo "$result";
    		}
    Code (markup):
    outputs sth like: dbca
    and i would like to make a variable $sth = dbca

    so how can i write sth like $sth = foreach ($results as $result) { echo "$result";}

    thanks for your help! I am still learning php.
     
    Alex69, Jan 20, 2011 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    $sth = "";
    foreach ($results as $result) {
    $sth .= $result;
    }
     
    bartolay13, Jan 20, 2011 IP
  3. Alex69

    Alex69 Active Member

    Messages:
    312
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    thanks a lot :)
     
    Alex69, Jan 21, 2011 IP
  4. robsnob

    robsnob Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I would recommend using:

    
    $sth = implode('', $results);
    
    PHP:
     
    robsnob, Jan 21, 2011 IP
  5. weppaster

    weppaster Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    try pls this codes.
     
    weppaster, Jan 21, 2011 IP
  6. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #6
    this is same he posted :D
    $results = array("a","b","c","d");
    shuffle($results);
    
    foreach ($results as $result) {
     $sth .= $result;
    }  
    PHP:
    and you're done
     
    G3n3s!s, Jan 22, 2011 IP