1

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[]

6
  • Doesn't in_array() do what you want to find a string in an array? Commented Apr 25, 2016 at 16:21
  • 3
    Things would be so much easier if you normalized your database instead of storing everything in one column. Commented Apr 25, 2016 at 16:22
  • his both comments are correct Commented Apr 25, 2016 at 16:23
  • Loop through $resultArray. Split the element first on =, then split the second part of that on ;. Then use in_array() to search that array for "yellow". Commented Apr 25, 2016 at 16:24
  • Barmar- First: i can create new table in database only with profie but there will be about 40 kolumns. It is still ok ? just asking. Second: but if yellow will be in firstset too ? So what will be better? create new table or playing around with it? Commented Apr 25, 2016 at 16:28

4 Answers 4

1

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;
Sign up to request clarification or add additional context in comments.

3 Comments

because of break don't you think if next indexes also have yellow that will ignored?
@kafus if your array have more than one time same value then only first one will come and others will skip. Check once. for example:- resultArray ( [1] firstset=one;two;three [2] secondset=blue;yellow;red; [3] thirdset=width;height; [4] thirdset=width;height;yellow; )
yes but first of all i'll get variable with secondset=blue;yellow;red; then i can look for my 'yellow'. There is no option that i can get 2x secondset
0

Are you looking something like this.

$result = Array (

'1' =>'firstset=one;two;three',
'2' =>'secondset=blue;yellow;red;',
'3' =>'thirdset=width;height;'

);
foreach($result as $key=>$value){
    if(strpos($value,'yellow')){
        $secondset = $value;
        break;

    }

}

$result1 = explode('=', $secondset);
$result2 = explode(';', $result1[1]);
list($a,$b,$c) = $result2;
echo $b;

Comments

0

Another solution without foreach:

$resultArray =  [
    'firstset=one;two;three',
    'secondset=blue;yellow;red;',
    'thirdset=width;height;',
];

// all the results matching secondset
$result = preg_grep('/.*secondset.*/', $resultArray);
// if you have only one result
$resultString = reset($result);

Then you can apply your explode on $resultString. If you have several results, like many string in your array with "secondset" on it, you will be able to process them in the array $result.

$result -> array(1) {
  [1] =>
  string(26) "secondset=blue;yellow;red;"
}

$resultString -> string(26) "secondset=blue;yellow;red;"

Comments

0

You can use array_filter function with custom anonymous function.

$resultArray  = array("firstset=one;two;three", "secondset=blue;yellow;red;", "thirdset=width;height;");  

$matches = array_filter($resultArray, function ($haystack) {
    return strpos($haystack, 'secondset=') !== false ? true : false;
});

   // output => Array ( [1] => secondset=blue;yellow;red; )
print_r($matches);

To obtain all array keys (in your case only one):

// output => Array ( [0] => 1 )
print_r(array_keys($matches));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.