preg_grep - empty array() returned - help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bogdan
Forum Commoner
Posts: 27
Joined: Wed May 31, 2006 10:07 am
Location: Timisoara, Ro

preg_grep - empty array() returned - help

Post by bogdan »

Hi,
I have this array:

Code: Select all

$albacore = array(
   [a] => apple
   [b] => banana
   [{k}] => lemon
   [c] => squid
   [d] => pear
)
I want to look into the array, get the elements with [{k}], print them out.

Code: Select all

$results = preg_grep("/\{k\}/",$albacore);
print"<pre>";
print_r ($results);
print"</pre>";
Unfortunately for me, this prints:

Array
(
)

Regards, B
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: preg_grep - empty array() returned - help

Post by Benjamin »

Why not...

Code: Select all

$albacore = array(
   [a] => apple
   [b] => banana
   [k] => lemon
   [c] => squid
   [d] => pear
)

Code: Select all

echo $albacore['k'];
bogdan
Forum Commoner
Posts: 27
Joined: Wed May 31, 2006 10:07 am
Location: Timisoara, Ro

Post by bogdan »

{k} is a template tag in a list of params somwehere, which I am not supposed to alter in any way.
I just have this array of params => values in the end, From which I want to extract the params that have not been set, e.g. they have the tag {k} instead of a set value (something like that).
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Oh wow I never knew such a function existed :) That could make some of my scripts more efficient :)

preg_grep() seems to look for array *values* not keys.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

To followup, the poor man's preg_grep_keys() would be something like:

Code: Select all

function preg_grep_keys($pattern, $input)
{
    $ret = array();
    foreach ($input as $k => $v)
    {
        if (preg_match($pattern, $k)) $ret[$k] = $v;
    }
    return $ret;
}
bogdan
Forum Commoner
Posts: 27
Joined: Wed May 31, 2006 10:07 am
Location: Timisoara, Ro

Post by bogdan »

d11wtq wrote:Oh wow I never knew such a function existed :) That could make some of my scripts more efficient :)

preg_grep() seems to look for array *values* not keys.

Thanks, that cleared a few things. Seems I have to find another way.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

What about array_keys()

Also, i presume in the array, you will on have one key that is called {k} otherwise they would be overwritten!?
bogdan
Forum Commoner
Posts: 27
Joined: Wed May 31, 2006 10:07 am
Location: Timisoara, Ro

Post by bogdan »

There may be more that are not over written :( , keys: white{k}, blue{k}....

I am inclined to look one level above and get them before they are made array keys :D

Thanks for the array_keys() - I seem to be able to locate the exact position of a set key.
Post Reply