Question

I work with Silex Framework and I would like to add $age to $result after the calculation of the age Is that possible??

$result = $app['db']->fetchAll($sql);

 foreach ($result as $DN) {
    $DT = $DN['dateN'];
        $am = explode('-', $DT);
        $an = explode('/', date('Y/m/d'));
        $age = $an[0] - $am[0];
        echo $age;
  }

 return $app['twig']->render('page.html',array('list' => $result,'age'=>$age));
Was it helpful?

Solution

You can try to use a reference (with &) instead of a copy and add the item:

foreach ($result as &$DN) {
    $DT = $DN['dateN'];
    $am = explode('-', $DT);
    $an = explode('/', date('Y/m/d'));
    $age = $an[0] - $am[0];
    $DN['age'] = $age;
}

OTHER TIPS

Assuming your fetchAll returns an array of arrays "referencing" your resultset

foreach($result as $index => $row) {
    // ....
    $result[$index] = $age;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top