Retain elements in array if associative key starts with specific substring

Viewed 15965

If I have array like:

array [
       y => 35
       x => 51
       z => 35
       c_3 => 4
       c_1 => 54
       c_6 => 53
       c_9 => 52
] 

I want to get array of:

array [c_3=>4, c_1=>54, c_6=>53, c_9=>52]

How can I filter out the elements which do not have keys starting with c_?

7 Answers

I have tried the above solution but none of the solutions above worked for me in PHP 5.6

So, I tried and used different solution which worked for me...

$subArray = array();

foreach($arryDetails as $key => $value)
  if(preg_match('/^c_/',$key))
    $subArray[$key] = $value;

print_r($subArray);
Related