Extracting components of a street address I have got the following function in my class that I think can be further be improved but I'm lacking ideas how:

...
private function populateAddressVars()
{
    if (!$this-&gt;service_results || !$this-&gt;service_results-&gt;results[0]) {
        return false;
    }

    foreach ($this-&gt;service_results-&gt;results[0]-&gt;address_components as $component) {
        if (in_array('street_number', $component-&gt;types)) {
            $this-&gt;streetNumber = $component-&gt;long_name;
        }
        if (in_array('locality', $component-&gt;types)) {
            $this-&gt;locality = $component-&gt;long_name;
        }
        if (in_array('postal_town', $component-&gt;types)) {
            $this-&gt;town = $component-&gt;long_name;
        }
        if (in_array('administrative_area_level_2', $component-&gt;types)) {
            $this-&gt;country = $component-&gt;long_name;
        }
        if (in_array('country', $component-&gt;types)) {
            $this-&gt;country = $component-&gt;long_name;
        }
        if (in_array('administrative_area_level_1', $component-&gt;types)) {
            $this-&gt;district = $component-&gt;long_name;
        }
        if (in_array('postal_code', $component-&gt;types)) {
            $this-&gt;postcode = $component-&gt;long_name;
        }
        if (in_array('route', $component-&gt;types)) {
            $this-&gt;streetAddress = $component-&gt;long_name;
        }
    }
}
...


Can it be further improved?
To start with, your code doesn't make much sense to me. You're looping over $this-&gt;service_results-&gt;results[0]-&gt;address_components, and on each iteration, you seem to be setting all of the address values. If the address_components array contains 4 arrays, each setting all of the address vars, you're effectively overwriting the values, and are only left with the last set of values. More on this later, though (see "other niggles" below).

To answer your question:
Yes, you can improve this code. You have a rare case where a more readable implementation of your code actually becomes more performant, too.
The first thing to note is that the function you use the most (in_array), is implemented as a simple linear search of an array (hash table internally). This means it has a time complexity of O(n), n being the size of the array. Replacing it with a loop might seem to do the same thing, but you'll only go through the array once:

foreach ($this-&gt;service_results-&gt;results[0]-&gt;address_components as $component) {
    foreach ($component-&gt;types as $k)
    {
        switch ($k)
        {
            case 'street_number': $this-&gt;streetNumber = $component-&gt;long_name; break;
            case 'postal_town': $this-&gt;town = $component-&gt;long_name; break;
            case 'administrative_area_level_1': $this-&gt;district = $component-&gt;long_name; break;
            case 'postal_code': $this-&gt;postcode = $component-&gt;long_name; break;
            case 'route': $this-&gt;streetAddress = $component-&gt;long_name; break;
            case 'administrative_area_level_2':
                $k = 'country';//same as country, so reassign, don't break
            case 'country':
            case 'locality':
                $this-&gt;{$k} = $component-&gt;long_name;//key and property share the same name...
                break;
        }
    }
}


This already looks a tad better, but it still relies on us hard-coding all possible values for $component-&gt;types and assign the value of $component-&gt;long_name to the properties. I'd use a mapping array instead:

//outside all loops:
$map = array(
    'street_number' =&gt; 'streetNumber',
    'postal_town'   =&gt; 'town',
    'postal_code'   =&gt; 'postcode',
    'route'         =&gt; 'streetAddress',
    'country'       =&gt; 'country',
    'locality'      =&gt; 'locality',
    'administrative_area_level_1' =&gt; 'district',
    'administrative_area_level_2' =&gt; 'country'
);
//inner loop:
foreach ($component-&gt;type as $k)
{
    if (isset($map[$k]))
        $this-&gt;{$map[$k]} = $component-&gt;long_name;
    else
        //handle error, type not in $map: throw exception, log or "overload" $this (bad idea IMHO)
        $this-&gt;{$k} = $component-&gt;long_name;
}


Other niggles:
There are some other issues I have with your code. Two to be precise:

Data format:
Like I said before, you seem to be re-assigning the same properties over and over (or at least, there is a possibility you are). You might want to re-evaluate the data structure you have, if the $this-&gt;service_results-&gt;results[0]-&gt;address_components array looks something along the lines of:

[
    [
        'types' =&gt; ['street_number', 'postal_town', 'country',...],
        'long_value' =&gt; 'some value here'
    ],
    [
        'types' =&gt; ['street_number', 'postal_town', 'country',...],
        'long_value' =&gt; 'Another value'
    ]
]


Then your properties ($this-&gt;streetNumber, $this-&gt;town, $this-&gt;country,...) will all end up with the same value: "Another value" in this case. This is probably not what you want. Since each $component seems to have an array of type values, but only one real value (long_name), I suspect your data to look more like this:

[
    [
        'types' =&gt; ['street_number'],
        'long_value' =&gt; '10'
    ],
    [
        'types' =&gt; ['postal_town'],
        'long_value' =&gt; 'London'
    ],
    [
        'types' =&gt; ['country','administrative_area_level_1'],//alias for country
        'long_value' =&gt; 'UK'
    ]
]


Which means that, most of the time, traversing the entire types array is rather pointless, and so we can safely refactor the last snippet I suggested to break; once we've assigned our long_name value:

foreach ($component-&gt;type as $k)
{
    if (isset($map[$k]))
    {
        $this-&gt;{$map[$k]} = $component-&gt;long_name;
        break;//found the property to set, skip to next component
    }
}


If your data essentially does look like the first version (where the types array lists all possible types), then consider checking the value of long_name, because really: if the value represents a street number, you don't want to assign that value to the $this-&gt;country property, do you?
Add a var_dump($this-&gt;service_results-&gt;results[0]-&gt;address_components); to your question, so we know what type of data you're dealing with here.

Separation of concern:
Your class is, IMO, clearly breaking the SOLID principles. Single Responsability Principle (SRP) and, as a result, the separation of concern is not adhered to. The idea is that a class is a unit of code that has "at most one reason to change". That's a fancy way of saying that a class can have only 1 job. A class can be tasked to connect to a database, or perform cUrl request. But those classes should not be responsable for processing and storing the data it they obtain.

A PDO instance connects to the database, but does not handle prepared statements, or result sets. That's not its job, that's the reason for a PDOStatement to change. Neither of these two classes report errors, that's the job of the PDOException class.
Now look at your code. Your class might use another class instance to perform queries or curl requests, or it could contain the code that does this itself. If so: that's bad, because your class now does at least 2 things: fetching data and storing it (assigning to properties).

As if that weren't enough, the code you posted here also seems to take care of raw responses, extracting the data you're after and then storing it. So the class that contains the method you posted here has at least 2, and possibly even 3 "reasons to change". That's just plain evil.
What's more: your class doesn't only store the formatted data, it carries with it the raw data from which you extracted the address, the property $this-&gt;service_results. What is it for? You process the results, store the data you're after and either get rid of the raw data, or pass it on to another class/module that needs that data.

Post more code (as a separate review-request) if you want us to have a look at your class as a whole.