Matthieu McLaren
Matthieu McLaren

Reputation: 224

Finding a value in an array

I'm trying to find the best way to find a value in an array:

The value of $this->school_degree is retrieved from the Facebook Graph API. For the sake of this example, its value could be any (but only one of the following):

So my natural inclination was to do this:

 function EXPLODETEST () {
       $explode_degree = explode(" ", $this->school_degree);
       echo "$explode_degree[0]";
       echo "$explode_degree[1]";
       echo "$explode_degree[2]";
       echo "$explode_degree[3]";
       echo "$explode_degree[4]";
       echo "$explode_degree[5]";
       echo "$explode_degree[6]";
       }

At which point I'd have to create a really long if or statement to search each offset for the words computer science.

The endgame is to echo one statement if they're mastering in CS and echo another statement if they're not. What is the best way to go about this?

Upvotes: 0

Views: 82

Answers (1)

Rich Adams
Rich Adams

Reputation: 26584

If the degree always contains the words "Computer Science" could you not just search for that text in the string?

if (strpos($this->school_degree, "Computer Science") !== false)
{
    echo "Mastering in CS";
}
else
{
    echo "Not Mastering in CS";
}

Upvotes: 4

Related Questions