PHP: Can I get the index in an array_map function?

后端 未结 5 1963
甜味超标
甜味超标 2021-01-30 09:57

I\'m using a map in php like so:

function func($v) {
    return $v * 2;
}

$values = array(4, 6, 3);
$mapped = array_map(func, $values);
var_dump($mapped);
         


        
5条回答
  •  梦如初夏
    2021-01-30 10:47

    You can create your own map function using foreach:

    $val) {
            $result[$key] = $callback($key, $val);
        }
        return $result;
    }
    
    $values = array(4, 6, 3);
    $mapped = foreachMap('myCallback', $values);
    var_dump($mapped);
    
    

    try: https://3v4l.org/pmFlB

提交回复
热议问题