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
bogdan
Forum Commoner
Posts: 27 Joined: Wed May 31, 2006 10:07 am
Location: Timisoara, Ro
Post
by bogdan » Mon Jul 17, 2006 6:11 am
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
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Mon Jul 17, 2006 6:15 am
Why not...
Code: Select all
$albacore = array(
[a] => apple
[b] => banana
[k] => lemon
[c] => squid
[d] => pear
)
bogdan
Forum Commoner
Posts: 27 Joined: Wed May 31, 2006 10:07 am
Location: Timisoara, Ro
Post
by bogdan » Mon Jul 17, 2006 6:19 am
{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).
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Mon Jul 17, 2006 6:19 am
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.
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Mon Jul 17, 2006 6:21 am
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 » Mon Jul 17, 2006 6:22 am
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.
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Mon Jul 17, 2006 6:23 am
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 » Mon Jul 17, 2006 6:47 am
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
Thanks for the array_keys() - I seem to be able to locate the exact position of a set key.