Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:从Python字典中获取类似字符串的字符串_Php - Fatal编程技术网

PHP:从Python字典中获取类似字符串的字符串

PHP:从Python字典中获取类似字符串的字符串,php,Php,我有一个字符串,比如: {"Url":"http://localhost","DBName":"John_db","DBUser":"admin","Pass":"a"} 现在使用这个字符串,我想要URL、DBname、DBuser,并将它们传递给单独的变量,如: $DBName = 'John_db'; $DBUser = 'admin'; $Url = 'http://localhost'; $Pass = 'a'; 我是PHP新手,找不到任何解决方案来实现这一点,有人能

我有一个字符串,比如:

{"Url":"http://localhost","DBName":"John_db","DBUser":"admin","Pass":"a"}
现在使用这个字符串,我想要URL、DBname、DBuser,并将它们传递给单独的变量,如:

$DBName =  'John_db';
$DBUser =  'admin';
$Url    =  'http://localhost';
$Pass   = 'a';
我是PHP新手,找不到任何解决方案来实现这一点,有人能帮我吗?

“这个字符串”是JSON对象。用于获取包含所有值的数组,然后从中获取

$str = '{"Url":"http://localhost","DBName":"John_db","DBUser":"admin","Pass":"a"}';
$out = json_decode( $str, true );
$out
的结尾如下:

Array
(
    [Url] => http://localhost
    [DBName] => John_db
    [DBUser] => admin
    [Pass] => a
)

你不需要把每一个都分割成单独的变量。您可以将该JSON解码为一个数组或对象:

$str = '{"Url":"http://localhost","DBName":"John_db","DBUser":"admin","Pass":"a"}';
$arr = json_decode( $str, true );
现在您有了一个包含所有变量的关联数组:

Array(
  [ Url ] => "http://localhost",
  [ DBName ] => "John_db",
  ...
)
如果不将第二个参数指定为
json\u decode()
,则会得到一个常规对象:

$obj = json_decode( $str );
echo $obj->Url; // http://localhost
echo $obj->DBName; // John_db
参考-

现在你应该给你的Var加油了


此处的工作代码:

使用json\u解码函数,如下所示:

<?
$string = '{"Url":"http://localhost","DBName":"John_db","DBUser":"admin","Pass":"a"}';
$array = json_decode( $string, true );

print_r($array);
?>


该字符串是一个JSON对象,您可以使用JSON_decode将该字符串转换为关联数组。

对于列表,您必须确保JSON的每个元素都位于正确的位置。你不能肯定这是真的。。错过了!我想您可以在使用
list()
之前对数组进行排序,但这过于复杂了……您是对的,但是JSON可能会错过一个参数,我们再次回到问题的开头:p这是真的。。。我想如果你不能保证所有的价值观都会一直存在,那么这真的不是一个选择。我真的很感谢你的努力!,现在我有一个问题,在字符串中键有一个空格,比如{“DB Name”:“John_DB”}。那一次我不能从$obj中取一个值,你有什么解决方案吗?在这种情况下,你应该通过传递
true
json\u decode()
来创建一个关联数组(我的第一个示例)。然后,您可以使用以下命令访问该值:
$arr[“DB Name”]
。很高兴提供帮助!快乐编码!:)再多解释一点,也许再举一些例子就能真正帮助改进这个答案。@Lix,当我发布并开始扩展时,许多其他人也给出了答案。
<?
$string = '{"Url":"http://localhost","DBName":"John_db","DBUser":"admin","Pass":"a"}';
$array = json_decode( $string, true );

print_r($array);
?>