2016-05-21 43 views
1

PHP:PHP,Laravel - 通配符鍵搜索數組並返回數組結果

$getallvalues 

讓我從會話數組:

array:7 [▼ 
    "_token" => "ZCP63uasUQHl948oVVDG7ZO4x33" 
    "_previous" => array:1 [▶] 
    "flash" => array:2 [▶] 
    "key1" => "1" 
    "key2" => "2" 
    "id-2" => "2" 
    "id-3" => "3" 
] 

如何獲取一個新的陣列通配符鍵「 ID-」給我的結果:

array:2 [▼ 
     "id-2" => "2" 
     "id-3" => "3" 
    ] 

我tryed一些PHP的方法,如:array_values,in_array等,但沒什麼克按我的預期工作。

+1

看到這個[https://glot.io/snippets/eew9bqe5hb](https://glot.io/snippets/eew9bqe5hb) –

+1

@GarvitMangal您可以使用'array_intersect_key',而不是在foreach提高你的答案循環 – Scopey

+0

@Scopey謝謝你指着我 –

回答

1

我發現了一個更好的解決方案,但沒有使用2行的foreach代碼!

$resultsession = preg_grep('/^id-[\d]*/', array_keys($getallsession)); 
$result = array_flip($resultsession);