I have an array that looks like this:
array(
\'abc\' => 0,
\'foo-bcd\' => 1,
\'foo-def\' => 1,
\'foo-xyz\' => 0,
// ...
)
foreach($arr as $key => $value)
{
if(preg_match('/^foo-/', $key))
{
// You can access $value or create a new array based off these values
}
}
In addition to @Suresh Velusamy's answer above (which needs at least PHP 5.6.0) you can use the following if you are on a prior version of PHP:
<?php
$input = array(
'abc' => 0,
'foo-bcd' => 1,
'foo-def' => 1,
'foo-xyz' => 0,
);
$filtered = array_filter(array_keys($input), function($key) {
return strpos($key, 'foo-') === 0;
});
print_r($filtered);
/* Output:
Array
(
[1] => foo-bcd
[2] => foo-def
[3] => foo-xyz
)
// the numerical array keys are the position in the original array!
*/
// if you want your array newly numbered just add:
$filtered = array_values($filtered);
print_r($filtered);
/* Output:
Array
(
[0] => foo-bcd
[1] => foo-def
[2] => foo-xyz
)
*/
Modification to erisco's Functional approach,
array_filter($signatureData[0]["foo-"], function($k) {
return strpos($k, 'foo-abc') === 0;
}, ARRAY_FILTER_USE_KEY);
this worked for me.