Is there a better way to search through $_SESSION variables (or any array) for a particular string than:
foreach($_SESSION as $k => $v){
if(strstr($k, 'p_')){
Thanks.
edit: My keys will look similar to:
p_123
p_456
i_123
…
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If your
$_SESSIONstructure must stay “as is”, IMO it is Ok.However if all ‘p_’ elements could go under an array index like
$_SESSION['p'] = array('key1' => 'val1', ...), you could retrieve all ‘p’ elements at once.BTW this is only micro optimization, go with the structure you’re fine with.
EDIT: Just be careful with
strstr(): if one day you must store keys like i_123_p_456 into your$_SESSIONarray, you should switch toif (strpos($k, 'p_') === 0).