Kafus Kafus - 7 months ago 19
PHP Question

PHP find string in array

I have a column profile in MySql

firstset=one;two;three|secondset=blue;yellow;red;|thirdset=width;height;|


(expample) I need to get:
yellow
from
secondset
to variable

What i wrote:

... MySql query etc ...
...
if ($r_select['profile'] != null)
{
$resultSelect = $r_select['profile'];

$resultArray = explode('|', $resultSelect);


So now i have:

resultArray (

[1] firstset=one;two;three
[2] secondset=blue;yellow;red;
[3] thirdset=width;height;

)


I need to find where in
resultArray[]
is
secondset=
then save this in variable- i want to get

$found = "secondset=blue;yellow;red;"


I know how to
explode
variable but i cant find string in array[]

Answer

Try this:

if ($r_select['profile'] != null) {

    $resultSelect = $r_select['profile']; 
    $resultArray  = explode('|', $resultSelect); 

    foreach ($resultArray as $data) {

        if (strpos($data, 'secondset') !== FALSE) {
            $found = $data;
            break;
        }
    }
}
echo $found;

Result:

secondset=blue;yellow;red;
Comments