2

For example, I have a function like this:

function loopValues()
{
    $a = array('a','b','c');
    foreach($a as $b)
    {
        $c = $b.'e';
        echo $c;
    }
}

How could I return its value aebece in an array like ('ae','be','ce')?

4 Answers 4

3
$a = array('a','b','c');
$b = array_map(function($ele) {
    return $ele .= 'e';
}, $a);

See it in action

Sign up to request clarification or add additional context in comments.

Comments

2

Try

function loopValues()
{
    $a = array('a','b','c');
    $result = array();
    foreach($a as $b){
        $result[] = $b.'e';
    }
    return $result;
}

$r = loopValues();
print_r($r);

See demo here

Comments

1

Simple, try this:

function loopValues()
{
    $a = array('a','b','c');
    $r = array();
    foreach($a as $b)
    {
        $c = $b.'e';
        $r[] = $c;
    }
    return $r;
}

Comments

0
function loopValues(){
   $a = array('a','b','c');
   for($i=0;$i<count($a);$i++){
      $a[$i] .= 'e';
   }
   return $a;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.