checking a name of an array element

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
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

checking a name of an array element

Post by pelegk2 »

i have an array for example like this :
Array ( [r_id] => 014 [el_id] => 83 [rd_num] => 3 [order_id] => 10322 [el_id_3] => 76_3 [PHPSESSID] => bfc85ce685af64607fa723e77bcfa259 ) 1
and i want to go over the array and check all the cells
that there index start with "el_id_" to get each cell its full name for example : el_id_3 and its value 76_3
how can i do this?
thnaks in advance
peleg
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

regexes are probably the quickest code for it:

Code: Select all

<?php

$data = Array ( 'r_id' => 014, 'el_id' => 83, 'rd_num' => 3, 'order_id' => 10322, 'el_id_3' => '76_3', 'PHPSESSID' => 'bfc85ce685af64607fa723e77bcfa259' );

$captured = array();
foreach($data as $k => $v)
{
  if(preg_match('#^el_id_.+$#i',$k))
    $captured[$k] = $v;
}

?>
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

10X:) ....can u just tell.....

Post by pelegk2 »

thnaks alot
can u just tell me why is the :
#^
for what is that?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

# is the character I used to surround the regex (required for preg* functions), ^ tells it to match only from the beginning of the string.
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

Post by pelegk2 »

ok thnaks alot!
Post Reply