Search code examples
phpregexarraysarray-key

How to get max number from array key matching a pattern


I have an array

$_images['image[1]'] = image1.jpg
$_images['image[2]'] = image2.jpg
$_images['image[3]'] = image3.jpg
$_images['image[4]'] = image4.jpg

How do I find the max number/ count in the key is 4 which matches a pattern image[].

Appreciate any help.


Solution

  • Thanks to Marc B !

    count(preg_grep('/images[([0-9]+)]/', $_images));