2016-04-13 37 views
0

我有一個Map如:哈克迭代(圖)在地圖

$m = Map { 
    'sort' => 'created', 
    'order' => 'desc', 
} 

我希望把它轉換成字符串:

'sort:created order:desc' 

我可以用數組做到這一點,如this SO answer解釋:

implode(' ', array_map(($k, $v) ==> $k.':'.$v, array_keys($m), $m)) 

我讀過Map::items的文檔,試過了:

$m->items()->map(($key, $value) ==> print($key)) 

但是這不會打印任何東西。

我正在尋找像數組一樣的oneliner,沒有循環。

回答

4

map()的參數是一個函數,只需要一個參數;如果您運行typechecker,它會告訴你:

test.php:9:20,20: Invalid argument (Typing[4039]) 
    /tmp/hh_server/hhi_1ebd4af3/hhi/interfaces.hhi:363:27,44: Number of arguments doesn't match 
    test.php:9:20,20: Because of this definition 

你想要的是mapWithKey()https://3v4l.org/GF69D

$m->mapWithKey(($key, $value) ==> print($key)); 

您也可以使用您所使用的陣列相同的代碼:https://3v4l.org/mSREI

+0

哇3v4l.org網站很酷! –

0

使用:

implode(' ', $m->mapWithKey(($k, $v) ==> $k.':'.$v))