Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hsntngr's avatar

How to get all values from session which start with certain key

As the title, I want to get all values from session which start with certain value.

For example, I'm counting post views in my project and I'm storing them on session. Then I want to reach all stored viewed values on session..

Session::put("views.".$post->id);

How can I get all stored views ?

is there any way to do it something like below ?

Session::get("views.*");
0 likes
1 reply
lostdreamer_nl's avatar

You could try this:

// get all session data
$data = session()->all();
// create multi dimensional array instead of the dot notation
$array = array();
foreach ($data as $key => $value) {
    array_set($array, $key, $value);
}
// grab the key you want:
$views = $array['views'];

dd($views);

Please or to participate in this conversation.