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条)

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像