2

From the following array:

area = Array
    (
        sur12 => 2
        abb12 => 5
        sur13 => 6
        abb13 => 4
        sur14 => 8
        abb14 => 4
        sur15 => 2
        abb15 => 5
        abb16 => 8
        abb17 => 5
        abb18 => 4
        abb19 => 2
        abb20 => 6
        abb21 => 9
        abb22 => 5
        abb23 => 9
        abb24 => 8
        sur_count15 => 4
        abb_count24 => 2
    )

how do I access area['sur_count15'] if I only know the prefix sur_count, or area['abb_count24'] if I only know the prefix abb_count?

Is there any way to do this in PHP, like, e.g. in CSS where I can access an ID that starts with abc_count by using [id*="abc_count"]?

The prefix is unique to the first 3 characters.

Ana Ban
  • 1,395
  • 4
  • 21
  • 29
  • Have you considered using `array_keys($area)`? See http://stackoverflow.com/questions/2471120/php-function-array-key-exists-and-regular-expressions – azhrei Jul 18 '12 at 06:18

3 Answers3

1

I ended up using the answer to my other question over at How to get a subset of $_POST array with keys starting with a prefix. If only I could restructure the array, but that's a no-go for now. Thanks, y'all.

Community
  • 1
  • 1
Ana Ban
  • 1,395
  • 4
  • 21
  • 29
0

one of the solution to your above problem could be as below but i strongly advise you to look for another solution because this one may not be very optimizable solution :

for(int $i=0;$i<30;$i++)
{
  if(in_array('sur_count' . $i,$area))
  {
    //your code here
  }
  if(in_array('abb_count' . $i,$area))
  {
    //your code here
  }
}

may be this could help you a bit.....

gj-
  • 1
0

Try getting the array_keys, then searching this for a match using regexp /sur_count[0-9]*/ or similar.

See here for more details: php function array_key_exists and regular expressions

Community
  • 1
  • 1
azhrei
  • 2,303
  • 16
  • 18