duankuangxie9070 2014-12-05 00:01
浏览 52
已采纳

从PHP中的函数返回的数组中的Echo键值

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;
 }
  • 写回答

2条回答 默认 最新

  • douyi1899 2014-12-05 00:07
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要