1

I have a function like the below one that will select from the DB some different values. One of these values needs to be edited before passing the array to other functions.

The problem is that I don't know how to return the array with the edited element. How can I edit a value in the array and return the full array but with the edited element?

function eGetDashboard($eID, $pdo) {
    $dashboard = PDOjoin('event', 'event.*, join_category_event.*, join_event_user.*, _dashboard.*, _dashboard_icons.*', array(
        array('LEFT JOIN' => 'join_category_event', 'ON' => 'event.id_event = join_category_event.id_event'),
        array('LEFT JOIN' => 'join_event_user', 'ON' => 'event.id_event = join_event_user.id_event'),
        array('LEFT JOIN' => '_dashboard', 'ON' => 'join_category_event.id_category = _dashboard.id_category'),
        array('LEFT JOIN' => '_dashboard_icons', 'ON' => 'join_category_event.id_category = _dashboard_icons.id_category')
            ), array('event.id_event' => $eID), $pdo);
    foreach ($dashboard as $eDashboard)
        $label =  eSetDashboardLabel($eDashboard['multimedia_descr'], getLang());
    return $dashboard;
}

function eSetDashboardLabel($label, $lang) {
        $label = explode(";", $label);
        foreach ($label as $labels) {
            if (substr($labels, 1, 2) == $lang)
                return substr($labels, 4);
        }
    }
2
  • What element do you want to edit? Commented Jul 4, 2013 at 10:19
  • @n1te $eDashboard['multimedia_descr'] Commented Jul 4, 2013 at 10:19

1 Answer 1

3

By using a Reference (note: &) to the element, any changes will affect the original element:

foreach ($dashboard as &$eDashboard) {
    $eDashboard['multimedia_descr'] = eSetDashboardLabel($eDashboard['multimedia_descr'], getLang());
}

Alternatively, you can capture the key in the loop and reference the array:

foreach ($dashboard as $key => $eDashboard) {
    $dashboard[$key]['multimedia_descr'] = eSetDashboardLabel($eDashboard['multimedia_descr'], getLang());
}
Sign up to request clarification or add additional context in comments.

1 Comment

You win some, you lose some. I'm sure you'll beat me next time :)

Your Answer

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