PHP函数不返回键

PHP函数不返回键,php,wordpress,Php,Wordpress,请告诉我我做错了什么。。。使用wordpress 4.7.1的PHP版本5.6.27。我创建了一个自定义插件。“我的页面”返回结果为“失败”,尝试了两个不同的页面标题,两个都返回“失败”,这表明函数中没有获取页面标题(位置),函数返回else失败 $locations = array( array( 'location' => 'About', 'telephone' => '0121 34838383', 'emai

请告诉我我做错了什么。。。使用wordpress 4.7.1的PHP版本5.6.27。我创建了一个自定义插件。“我的页面”返回结果为“失败”,尝试了两个不同的页面标题,两个都返回“失败”,这表明函数中没有获取页面标题(位置),函数返回else失败

    $locations = array(
    array(
        'location'  => 'About',
        'telephone' => '0121 34838383',
        'email'     => 'example@example.com'
    )
);

function telephone_shortcode() {
    global $locations;
    $title = get_the_title();
    $key = array_search($title, array_column($locations, 'location'));
    if ($key)
        return $locations[$key]['telephone'];
    else
        return 'fail';
}
add_shortcode('telephone', 'telephone_shortcode');
[电话]-返回“失败”

如果要搜索的元素是数组中的第一个元素,则返回0。0==false(在php中查找“truthy values”),将if语句中的检查更改为

if($key !== false)
其他一切都可以保持不变。使用
==
告诉php检查值是否完全匹配。

如果要搜索的元素是数组中的第一个元素,则返回0。0==false(在php中查找“truthy values”),将if语句中的检查更改为

if($key !== false)

其他一切都可以保持不变。使用
==告诉php检查值是否完全匹配。

对于此配置,当您只有一页“关于”结果时:

array_search($title, array_column($locations, 'location')); 
是0。当您登记时:

if ($key)
对于“if”,$key参数等于false(因为其值为零)。将此函数设置为:

function telephone_shortcode() {
    global $locations;
    $title = get_the_title();
    $key = array_search($title, array_column($locations, 'location'));
    if (false !== $key)
        return $locations[$key]['telephone'];
    else
        return 'fail';
}

一切都开始起作用了。对于教育,请阅读PHP Dock:

了解此配置,当您只有一页“关于”此配置的结果时:

array_search($title, array_column($locations, 'location')); 
是0。当您登记时:

if ($key)
对于“if”,$key参数等于false(因为其值为零)。将此函数设置为:

function telephone_shortcode() {
    global $locations;
    $title = get_the_title();
    $key = array_search($title, array_column($locations, 'location'));
    if (false !== $key)
        return $locations[$key]['telephone'];
    else
        return 'fail';
}
一切都开始起作用了。关于教育,请阅读PHP Dock: