PHP编辑数组的元素并返回包含已编辑元素的数组

时间:2013-07-04 10:14:56

标签: php arrays

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

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

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

1 个答案:

答案 0 :(得分:3)

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

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