Preg请参阅json Multi Dimension字符串

时间:2015-02-24 17:18:09

标签: php json

我有这样的字符串:

<?php
echo $str_is;
?>

结果:

[["id_is","'bG1saW50YXMgbWFoYWthbTMyMTI'"],["date_is","'2015-2-25'"]]

我需要得到这样的数组:

$thearray["id_is"]="'bG1saW50YXMgbWFoYWthbTMyMTI'"
$thearray["date_is"]="'2015-2-25'"

我尝试使用(即时新手使用代码preg_match):

$thearray = preg_match('/[(\w+)/],([(\w+)/]/', $str_is, $matches);

1 个答案:

答案 0 :(得分:0)

这是一种方法:

$str_is = <<< EOS
[["id_is","'bG1saW50YXMgbWFoYWthbTMyMTI'"],["date_is","'2015-2-25'"]]
EOS;

// Convert the JSON string to an array
$array = json_decode($str_is, true);

// Loop the array adding to $thearray
foreach ($array as $pair) {
   // First element = key (e.g. "id_is")
   // Second element = value (e.g. "'bG1saW50YXMgbWFoYWthbTMyMTI'")
    $thearray[$pair[0]] = $pair[1];
}

echo $thearray['id_is'], PHP_EOL;
echo $thearray['date_is'], PHP_EOL;

输出:

'bG1saW50YXMgbWFoYWthbTMyMTI'
'2015-2-25'