Vikram Anand Bhushan
Vikram Anand Bhushan

Reputation: 4886

How to count the number of occurence of images in an array REGEX

I have an array something like this

ARRAY[0][DATAROOT.PROPIEDADES.FOTO1][0]
ARRAY[0][DATAROOT.PROPIEDADES.FOTO2][0]
ARRAY[0][DATAROOT.PROPIEDADES.FOTO3][0]
ARRAY[0][DATAROOT.PROPIEDADES.FOTO4][0]

I have to write a script to count the number of the photos in the array:

$x = "DATAROOT.PROPIEDADES.FOTO";

$ct = 8; // This is the number of image I am passing fixed variable

I want to get that using the array I need to find out a way that will count the array

that has column name DATAROOT.PROPIEDADES.FOTO with 1, 2,3 etc in the end.

This I am not able to do

for($tt=1;$tt<=$ct;$tt++)
{   
    $k=$x.$tt;     
    $result[]=$this->ARRAY[0][$k][0];
}

Can any one help me in this .

Thanks in Advance

Upvotes: 0

Views: 54

Answers (1)

Kevin
Kevin

Reputation: 41885

As an alternative, you could just get those elements inside the parent array whose keys has that substring:

$x = 'DATAROOT.PROPIEDADES.FOTO';
foreach($this->ARRAY[0] as $k => $value) {
    if(strpos($k, $x) !== false) {
        $result[] = $value[0];
    }
}

Or:

foreach($this->ARRAY[0] as $k => $value) {
    if(preg_match('~DATAROOT\.PROPIEDADES\.FOTO\d+~', $k)) {
        $result[] = $value[0]
    }
}

Upvotes: 1

Related Questions