Vasilis Greece
Vasilis Greece

Reputation: 907

Php, Laravel - search array with wildcard key and return array results

php:

$getallvalues

Gives me the array from session:

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

How to retrieve a new one array with wildcard key "id-" to give me the result:

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

I tryed some php methods like: array_values, in_array etc but nothing works as I expected.

Upvotes: 1

Views: 935

Answers (1)

Vasilis Greece
Vasilis Greece

Reputation: 907

I found a better solution without foreach code in 2 lines only!!

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

Upvotes: 1

Related Questions