【问题标题】:Array not being set in switch in my class在我的课堂上没有在 switch 中设置数组
【发布时间】:2023-03-28 12:00:02
【问题描述】:

我正在为我的网站设置一个搜索类,但我遇到了一些问题,但我不知道为什么它不起作用。

这是课程

<?php

class search
{
    public $location = array();
    public $keywords = array();

    /*
    construct method
    accepts $location and $keywords
    */
    public function __construct($location, $keywords='')
    {
        $this->location = $this->format_location($location);

    }

    /*method format_location
    accepts location value, determines its type... city, state, zip code
    returns array of properly formatted location values
    */
    public function format_location($input)
    {

        // check if location contains a zip code
        $zip_pattern = "/\d{5}/";
        $str = $input;
        preg_match($zip_pattern,$str,$regs);
        // if we do have a zip code lets put it in the location array
        (!empty($reg[0])) ? $this->location['zip'] = $reg[0] : '';

        // if we don't have a zip let's check for one of our pre=formatted locations, this will avoid unessesary regular expressions and database calls
        if(!$this->location['zip']) {

            $swinput = strtolower($input);
            switch($swinput) {
                case "chicago, il":
                echo 'hello';
                    $this->location[] = array('zip'   => 60601,
                                            'city'  => 'Chicago',
                                            'state' => 'IL',
                                            'lat'   => '41.8781136',
                                            'lon'   => '-87.629798',
                                            );
                                            break;
                case "naperville, il":
                    $this->location[] = array('zip'   => 60564,
                                            'city'  => 'Naperville',
                                            'state' => 'IL',
                                            'lat'   => '41.785863',
                                            'lon'   => '-88.147289',
                                            );
                                            break;
            }

        }

    }

}

?>

然后我就是这样称呼它的

require("lib/search/class.search.php");
$lo = new search('chicago, il');
print_r($lo->location);

问题是在 location 属性中没有设置任何内容。有什么想法吗?

【问题讨论】:

  • 我不知道它是否会导致您的错误,但if (!$this-&gt;location['zip']) 表示if ($this-&gt;location['zip'] == false)。您想知道是否设置了zip 键,因此请使用if (isset($this-&gt;location['zip']))

标签: php arrays class object


【解决方案1】:

尝试使用

/*
construct method
accepts $location and $keywords
*/
public function __construct($location, $keywords='')
{
    $this->format_location($location);

}

而不是

$this->location = $this->format_location($location);

因为 format_location 不返回任何内容。

【讨论】:

  • 哦,伙计,我觉得自己很想念那个。非常感谢修复它。
【解决方案2】:

换个方式

preg_match($zip_pattern,$str,$regs);
// if we do have a zip code lets put it in the location array
(!empty($reg[0])) ? $this->location['zip'] = $reg[0] : '';

preg_match($zip_pattern,$str,$reg);
// if we do have a zip code lets put it in the location array
(!empty($reg[0])) ? $this->location['zip'] = $reg[0] : '';

?

问候 最大

【讨论】:

    【解决方案3】:
    preg_match($zip_pattern,$str,$regs);
                                     ^---- S here
    
    (!empty($reg[0])) ? $this->location['zip'] = $reg[0] : '';
                ^--no S                              ^-- no S
    

    【讨论】:

      【解决方案4】:

      在声明开关时,我会尝试将 $swinput 转换为字符串:

      switch((string)$swinput) {
      

      如果这不起作用,则问题可能在于切换失败之前的条件。运行此程序时是否看到“hello”调试消息?

      【讨论】:

        猜你喜欢
        • 2012-08-11
        • 2018-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多