foreach result after close foreach

Discussion in 'PHP' started by vOlLvEriNe, Mar 21, 2014.

  1. #1
    Hi all,
    I have a prob,
    Let C,
    <?php
    $dp = array('apple','grapes');
    foreach ($dp as $d)
    {
    echo $d.',';
    }
    // out will apple,grapes,
    PHP:

    But I Want Like This
    <?php
    $dp = array('apple','grapes');
    foreach ($dp as $d)
    {
    $t = $d.',';
    }
    
    echo $t;
    PHP:
    for same result, How can I do ?
     
    Solved! View solution.
    vOlLvEriNe, Mar 21, 2014 IP
  2. #2
    <?php
    $dp = array('apple','grapes');
    $t = '';
    
    foreach ($dp as $d)
    {
    $t .= $d.',';
    }
    echo $t;
    PHP:
     
    Last edited: Mar 21, 2014
    AbstractChaos, Mar 21, 2014 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Is all you wanna do output the values of the array with comma separating them? You don't even need to do a foreach - you can just do:
    
    echo $t = join(', ',$dp);
    
    PHP:
    It will do the exact same thing.
     
    PoPSiCLe, Mar 21, 2014 IP
  4. vOlLvEriNe

    vOlLvEriNe Member

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    @PoPSiCLe I have a different case, fetching result, and I want Result After Foreach Ending, Have You Another Idea About Idea ?
     
    vOlLvEriNe, Mar 21, 2014 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    The solution above is good, but since I don't really know what you're trying to do, it's sort of hard to give proper advice - the example you gave is basically doing the exact same thing, only using more variables in the latter one, hence it doesn't make that much sense.
     
    PoPSiCLe, Mar 22, 2014 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #6
    My question would be why the blue blazes do you NOT want it the first way since it has far less overhead and far less memory use, since it's not wasting time and memory creating variables for NOTHING.

    Though I like @PoPSiCLe's using JOIN for it... I'd just skip the variable for nothing... unless you have something later on that needs it?

    Variables for NOTHING is bad code, and should be avoided. Just echo the bloody thing unless you REALLY plan on outputting it more than once.
     
    deathshadow, Mar 22, 2014 IP
  7. AbstractChaos

    AbstractChaos Member

    Messages:
    58
    Likes Received:
    4
    Best Answers:
    4
    Trophy Points:
    25
    #7
    I just figured it maybe a snippet of a larger bit of code and was simply to demonstrate problem the OP was having... It may not be just to return the data, besides if the original code worked for the OP the post wouldn't have been created.
     
    AbstractChaos, Mar 22, 2014 IP