convert arrays values into string

Discussion in 'PHP' started by playwright, Jul 14, 2010.

  1. #1
    hello!! is there any function that can convert every single value of an array into string??? I tried implode() but i think it converts the whole array into one string..maybe i'm wrong..this is what i'm trying to do..

    preg_match_all('/<img src\=[\"](.*?)[\"] (.*?)>/si',$x, $y);
    $count = count($y;
    for ($i=1;$i<=$count;$i++){
    $keys = implode("+",$y[$i]);
    $new = substr($keys,30,7);
    }

    any ideas??
     
    playwright, Jul 14, 2010 IP
  2. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #2
    Try list( ):
    $info = array('coffee', 'brown', 'caffeine');
    
    // Listing all the variables
    list($drink, $color, $power) = $info;
    echo "$drink is $color and $power makes it special.\n";
    
    PHP:
    :)
     
    Rainulf, Jul 14, 2010 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    Your loop needs to end at ($count-1), not $count.

    Are you trying to get each array sub-element into its own variable? The way your code works now is that it will put all the sub elements into one variable, $keys.
     
    ThePHPMaster, Jul 14, 2010 IP
  4. playwright

    playwright Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I want to process every single sub-element of the array. i tried to do it with foreach() but the result is the same..
     
    playwright, Jul 14, 2010 IP
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    If you want to process every single image, you do:

    
    preg_match_all('/<img src\=[\"](.*?)[\"] (.*?)>/si',$x, $y);
    
    foreach($y[1] as $img)
    {
        echo $img;
    }
    
    PHP:
    If you want each image in a string, lets say img_0, img_1,img_2, etc..:

    
    foreach($y[1] as $sub => $img)
    {
        ${"img_".$sub} = $img;
    }
    
    PHP:
    If this doesn't help, let me know what exact you are looking to end up with by example.
     
    ThePHPMaster, Jul 14, 2010 IP
  6. playwright

    playwright Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It works perfect!! i just needed to use
    foreach ($y[$i] as $img){}..
    Thanks !!
     
    playwright, Jul 14, 2010 IP