Question

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.

Was it helpful?

Solution 2

Thanks to Marc B !

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

OTHER TIPS

I don't really understand what you're asking but if you mean

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

Then just do echo count($_images);

If you mean

$images[1] = 'image1.jpg';
$images[2] = 'image2.jpg';
$images[3] = 'image3.jpg';
$images[4] = 'image4.jpg';

$_images = array();
array_push($_images, $images); //array of arrays

then do

echo count($_images, COUNT_RECURSIVE) - 1; //keep in mind your parent array must only have 1 array inside it, or you have to do -x where x = number of your second-level arrays 

If you mean something else, then you have to explain more...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top