tags:

views:

70

answers:

6

Hi

I have a array like this:

array('prefix1_field' => 34,
      'prefix1_anotherfield' => 345,
      'prefix1_andanotherfield' => 565,

      'anotherprefix_field' => 34,
      'anotherprefix_anotherfield' => 345,
      'anotherprefix_andanotherfield' => 565,

      'prefix3_anotherprefix_field' => 34, // <- 'anotherprefix' here should be ignored
      'prefix3_anotherfield' => 345,
      'prefix3_andanotherfield' => 565,
      ...
);

How can I make a function that checks if there are any fields in this array that start with prefix1_ for example?

+2  A: 
function check_array_key_prefix_exists($array, $key_prefix) {
  $keys = array_keys($array);
  foreach ($keys as $key) {
    if (0 == substr_compare($key, $key_prefix, 0, strlen($key_prefix))) {
      return true;
    }
  }

  return false;
}
Dominic Rodger
perfect. thank you
Alex
+2  A: 

Something like:

function check($arr,$prefix) {
        foreach($arr as $key => $value) {
                if(strcmp(substr($key,0,strlen($prefix)),$prefix)==0) {
                        return true;
                }
        }
        return false;
}
codaddict
If "prefix1_" is variable you could avoid having to also include the length by `if (strpos($key, $prefix) === 0)?`
Fanis
@Fanis - though that would do more work than is strictly necessary - since it'd keep looking for `prefix1_` even it isn't found at the beginning of `$key`.
Dominic Rodger
@Dominic I'm not sure. `=== 0` gets it to see if its index in that string is 0, ie the beginning of the string. Actually you pose an interesting point which made me question my habit of using strpos for this. I do suppose `strpos()` will search for $prefix in the entire $string, but if it's in the beginning then it will find it immediately and return back (int)0. If it's not, however, it will go through the entire $prefix wastefully. On the other hand, substr() creates a new variable using up memory. I reckon it's negligible though.
Fanis
@Fanis - how about `substr_compare`? Which is best will depend on the relative sizes of the substring being searched for and the string being searched in, and whether memory or CPU is what you care about.
Dominic Rodger
@Dominic right. I do believe both are pretty negligible in this case, but still some food for thought. Good feedback.
Fanis
+2  A: 

Why not use regular expressions?

function array_has_key_prefix( $array, $key_prefix ) {
  foreach($arr as $key => $value) {
    if( preg_match( "/^" . $key_prefix . "/", $key ) )
      return true;
  }
  return false;
}
Groovetrain
That would work, but seems like a fairly big hammer for doing simple string matching. Nice first answer though!
Dominic Rodger
Do regular expressions end up being slower than strcmp() or substr_compare()?
Groovetrain
yes, I'm curious about this too :)
Alex
@Groovetrain - I would imagine so - by the time your regular expression has been compiled - it's got to be quicker to just compare a bunch of bytes for equality than to run a regular expression over them. That said, I am not a profiling tool - go find out!
Dominic Rodger
A: 

Checks if a value exists in multidimensional array

moustafa
it isn't a multidimensional array. (it probably should be though)
Spudley
What? His array isn't multi-dimensional, and he's asking about keys not values!
Dominic Rodger
+1  A: 

I'd prefer to restructure the array as follows:

$data=array('prefix1'=>array(
                  'field'=>34,
                  'anotherfield'=>345,
               ),
            'prefix2'=>array(
                  'field'=>56,
               ),

...etc.

With that structure, you can just make a quick call to the standard PHP function array_key_exists().

With the structure you have, you'd have to basically roll your own alternative to array_key_exists(), which would involve a foreach() loop and explode() to break the key into bits.

Spudley
+1  A: 

I won't give much as informatio and I'll expilicitly write s.t. similar to the post of Dominique Roger.

function check_array_key_prefix_exists($array, $key_prefix) {
  $keys = array_keys($array);
  foreach ($keys as $key) {
    if (preg_match("#^$key_prefix", $array) {
      return true;
    }
  }

  return false;
}

I don't know if I've answerd your Question part because I didn't run it into he bwowser. Good Luck

Eric