foreach ($myarray as $value)
{
if (strpos($value,'mysearchstring'))
//add this $value to new array here
}
Also where would the second array need to be declared and how?
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.
To avoid warnings, the second array should be declared like so
This could be incorporated into your code like so
Your original code contained an error.
strpos returns and
falseif it doesn’t contain the needle, else it returns the index of the occurrence. Sostrpos($value,'mysearchstring')will return 0 if$valuestarts with'mysearchstring'. As PHP will convert0tofalseand visa versa, it will not get added to the array if we use the standard comparisons, so we need to explicitly check that it is false without type conversion (converting0tofalsefor example). To do this we use !== (Note the two=)s. This is represented in the code above.For more information on comparison operators in php, see the documentation.
EDIT
Regarding the commend and usage of
[], it ads the new element to the end of the arrayThe documentation states that:
So if we have an array