从Key分配变量名,其值为多维数组中的值(来自json)

时间:2016-03-31 07:04:04

标签: php arrays json multidimensional-array

我确定这是一个简单的答案,但我一直在搜索stackoverflow并尝试新事物几个小时,似乎无法解决这个问题。任何帮助表示赞赏!

我有一个看起来像这样的JSON对象:

[  
   {  
      "key":"user_agent",
      "value":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"
   },
   {  
      "key":"language",
      "value":"en-US"
   }
]

将来,这个json对象可能会改变,有更多的键/值对,改变键的顺序等。

在PHP中,我想为每个"键"分配$ variableName。并将值指定为"值"。

这是我到目前为止所得到的:

$arrayDecode = json_decode($data["someArray"]);

//count how many THIS WORKS
$iterations = count($arrayDecode, COUNT_RECURSIVE);

//loop DOES NOT WORK
for($i = 0; $i < $iterations; $i++) {
    $currentVariableName = $arrayDecode[$i]->key;
    $currentVariableName = $arrayDecode[$i]->value;
}

我尝试了类似$ {&#34;变量$ i&#34;}的内容。还尝试使用Foreach和提取功能。任何建议都非常感激,我已经厌倦了,并希望继续解决这个问题,这个问题可能对那些比我更了解PHP的人有明显的解决方案!

所以要明确,鉴于上述结果将是

$user_agent = "Mozilla/5.0 ...."
$language = "en-US"

2 个答案:

答案 0 :(得分:1)

$arrayDecode = '[  
   {  
      "key":"user_agent",
      "value":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"
   },
   {  
      "key":"language",
      "value":"en-US"
   }
]';

可以使用foreach和键值

来实现
$arrayDecode = json_decode($arrayDecode);

$collection = array();

if($arrayDecode){
    foreach($arrayDecode as $v){
        $collection[$v->key] = $v->value;
    }
}

答案 1 :(得分:0)

只需使用CustomListAdapterfriend adapter = new CustomListAdapterfriend(Friendsprofile.this, usersList); lv.setAdapter(adapter); 和简单json_decode()即可获取每个密钥和值:

foreach()

输出:

<?php
    $json = '[  
        {  
           "key":"user_agent",
           "value":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"
        },
        {  
           "key":"language",
           "value":"en-US"
        }
     ]';
    $arr = json_decode($json,true);
    $final = array();
    foreach($arr as $sub){
        $final[$sub['key']] = $sub['value'];
    }
    print_r($final);
?>

如果您期望多个包含相同键名的对(考试中有两个Array ( [user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36 [language] => en-US ) ),那么您必须创建多维数组:

user_agent

输出:

    <?php
        $json = '[  
            {  
               "key":"user_agent",
               "value":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"
            },
            {  
               "key":"language",
               "value":"en-US"
            }
         ]';
        $arr = json_decode($json,true);
        $final = array();
        foreach($arr as $key => $sub){
            $final[$key][$sub['key']] = $sub['value'];
        }
        print_r($final);
    ?>

更新: 为了将键作为变量,您可以在foreach循环中添加以下内容:

Array
(
    [0] => Array
        (
            [user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36
        )

    [1] => Array
        (
            [language] => en-US
        )

)