1

Sorry if this is a bit of a noob question, am still a beginner on php....

I need to echo the Country key from an array returned from a function. I have tried various way of trying to get the value I am wanting from the array, but each time do not get the results I want.

I want to set the 'country' value from the returned array to a variable to do an if argument on the value. Please help...

sample of what Im trying to do below -

<?php

$country = get_shipping_address()['country']

if ( $country=="GB" ) {

do this;

}

?>

Below is the function -

function get_shipping_address() {

    if ( ! $this->shipping_address ) {

         if ( $this->shipping_address_1 ) {

             // Formatted Addresses
             $address = array(
                 'address_1'     => $this->shipping_address_1,
                 'address_2'     => $this->shipping_address_2,
                 'city'          => $this->shipping_city,
                 'state'         => $this->shipping_state,
                 'postcode'      => $this->shipping_postcode,
                 'country'       => $this->shipping_country
              );

              $joined_address = array();

             foreach ( $address as $part ) {

                  if ( ! empty( $part ) ) {
                      $joined_address[] = $part;
                  }
             }

            $this->shipping_address = implode( ', ', $joined_address );
        }
     }

     return $this->shipping_address;
 }
1
  • You are not returning an array, your are returning a string $this->shipping_address = implode( ', ', $joined_address ); Commented Dec 5, 2014 at 0:07

2 Answers 2

2

Your issue is that you're doing this in your function:

foreach ( $address as $part ) {
    if ( ! empty( $part ) ) {
        $joined_address[] = $part;
    }
}
$this->shipping_address = implode( ', ', $joined_address );

What that does, is makes a string with all the values of your array. Example:

derp, derp, derp, derp, derp, derp

What you want is to return the $address variable.

return $address;

Making your function look like this:

function get_shipping_address() {
    $address = array();

    if (!$this->shipping_address) {

        if ($this->shipping_address_1) {

            // Formatted Addresses
            $address = array(
                'address_1' => $this->shipping_address_1,
                'address_2' => $this->shipping_address_2,
                'city' => $this->shipping_city,
                'state' => $this->shipping_state,
                'postcode' => $this->shipping_postcode,
                'country' => $this->shipping_country
            );

        }
    }

    return $address;
}
Sign up to request clarification or add additional context in comments.

6 Comments

won't be $address undefined in cases when if's conditions evaluate to false?
@Verhaeren That it will, but unless the OP can specify any need to cover that aspect, this answer covers his scope :)
Or address can have a default value as soon as the function starts.
Done, and it has a default empty array.
Good, but he also can get a undefined index error when he tries to check the returned array like this get_shipping_address()['country'].
|
0

Thanks Darren, ( and everyone else that commented ) That done the trick.

I have created a new function and changed it to only return the value I am needing, as I just need to know the country value. So have changed the function as below. Thanks for the help!!

function get_my_shipping_address() {

    if ( ! $this->shipping_address ) {

        if ( $this->shipping_address_1 ) {

            // Formatted Addresses
            $address = array(
                'address_1'     => $this->shipping_address_1,
                'address_2'     => $this->shipping_address_2,
                'city'          => $this->shipping_city,
                'state'         => $this->shipping_state,
                'postcode'      => $this->shipping_postcode,
                'country'       => $this->shipping_country
            );
        }
    }

    return $address['country'];
}

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.