簡體   English   中英

使用array_walk更改數組值

[英]Using array_walk to alter array values

這是我要在一組數組項上調用的函數:

function do_this_stuff( &$key ) {

    $lookups = array(
        'this' => 'that'
    );

    if (array_key_exists($key, $lookups)) {
        return $lookups[$key];
    } else {
        return ucwords(str_replace("_", " ", $key));
    }
}

並對其進行調用:

array_walk($data[$set][0], 'do_this_stuff');

如果$lookups數組中的任何內容位於array walk參數之一的數組中,我想替換其內容。 do_this_stuff函數有效,但是我嘗試過的所有操作do_this_stuff導致實際輸入數組值的更新。

您需要將更新后的值分配回$key ,而不返回它。

function do_this_stuff( &$key ) {

    $lookups = array(
        'this' => 'that'
    );

    if (array_key_exists($key, $lookups)) {
        $key = $lookups[$key];
    } else {
        $key = ucwords(str_replace("_", " ", $key));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM