首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将存储在JSON数组中的字符串转换为PHP中的变量名

将存储在JSON数组中的字符串转换为PHP中的变量名
EN

Stack Overflow用户
提问于 2018-06-05 04:16:10
回答 1查看 47关注 0票数 0

我已经创建了一个JSON文件,我将使用它作为应用程序全局配置的数据源。

摘录自json文件

代码语言:javascript
复制
 //Have not put the complete json file. No error in the file
 {
 "loginType":[
    {
        "name":"Facebook",
        "url":"#",
        "method":"",
        "label":"Continue with Facebook",
        "type":"social",
        "class":"",
        "icon":"",
        "callBack_url" : "fbLoginUrl",
        "providerButton":"<div class='fb-login-button' data-max-rows='1' 
        data-size='large' data-button-type='continue_with' data-use- 
        continue-as='true'></div>"        
    },
    {
        "name":"Twitter",
        "url":"#",
        "method":"logInWithTwitter()",
        "label":"Continue with Twitter",
        "type":"social",
        "class":"",
        "icon":"",
        "callBack_url" : "twitterLoginUrl",
        "providerButton" :""
      }
    ]
}

json文件中的callBack_url密钥有一个名称类似的变量,它的值是一个url,例如$twitterLoginUrl = "https://some.site.com/twitter_login?param1

代码语言:javascript
复制
$jsonData_signIn =json_decode
(file_get_contents(/path/to/oauth2_provider.json)); 
 $oauth2Provider = jsonData_signIn->loginType;
       foreach($oauth2Provider as $type){
         if($type->type == 'local' ){
           echo "<a href=\"{$type->callBack_url}\">{$type->label}</a>";
         }
    }    

对于上面的,作为链接的输出,我得到了例如<a href="$fbLoginURL">Continue with facebook</a>

代码语言:javascript
复制
echo "<a href=\"{${$type->callBack_url}}\">{$type->label}</a>";

我没有存储完整的URI的原因是我将动态生成一些参数。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-05 04:27:33

请看一下变量手册:http://php.net/manual/en/language.variables.variable.php

基本上,您只需将字符串变量名包装在${ }中,以使其行为类似于实际的变量。

代码语言:javascript
复制
$fbLoginUrl = 'https://www.facebook.com/v2.10/dialog/oauth?client_id=xxxxxxx&state=xxxxxxx&response_type=code&sdk=php-sdk-5.6.2&redirect_uri=some.site.com/fbLogin.php&scope=public_profile';
$json = '{"callBack_url" : "fbLoginUrl"}';
$decoded = json_decode($json);
echo ${$decoded->callBack_url};
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50688143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档