php array

hi

how can i search the string of a key in a array?

for example if i had $test = array(':colour'=>'red', ':name'=>'john', 'age'=>13);

how could i search the key of each value and check if it begins with a colon?

thanks
MK15Asked:
Who is Participating?
 
Greg AlexanderLead DeveloperCommented:
Here is a simple example of looping through it:

<?php
$test = array(':colour'=>'red', ':name'=>'john', 'age'=>13);

foreach($test as $k => $v){
	
	
	// now you have looped through each item, build in your search in your case a :
	if($k[0] == ':'){
		//here you would put all the matches into another array or echo them out
		echo "I have a colon ($k => $v)<br>";
		$colon_array[$k] = $v;
	}
}

// now show the colon items
echo "<pre>";
print_r($colon_array);
echo "</pre>";
?>

Open in new window

0
 
MK15Author Commented:
thank you, your help is appreciated:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.