1

我有一个类似下面的函数,它将从数据库中选择一些不同的值。在将数组传递给其他函数之前,需要编辑其中一个值。

问题是我不知道如何返回带有已编辑元素的数组。如何编辑数组中的值并返回完整的数组但使用编辑的元素?

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);
        }
    }
4

1 回答 1

3

通过对元素使用引用(注&:),任何更改都会影响原始元素:

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

或者,您可以捕获循环中的键并引用数组:

foreach ($dashboard as $key => $eDashboard) {
    $dashboard[$key]['multimedia_descr'] = eSetDashboardLabel($eDashboard['multimedia_descr'], getLang());
}
于 2013-07-04T10:23:42.560 回答