Page 1 of 1

checking a name of an array element

Posted: Sun Jul 04, 2004 10:22 am
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

Posted: Sun Jul 04, 2004 10:28 am
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;
}

?>

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

Posted: Sun Jul 04, 2004 10:37 am
by pelegk2
thnaks alot
can u just tell me why is the :
#^
for what is that?

Posted: Sun Jul 04, 2004 10:40 am
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.

Posted: Sun Jul 04, 2004 10:48 am
by pelegk2
ok thnaks alot!