用PHP解码Base64编码的数组 [英] Decoding an Base64 encoded Array in PHP
问题描述
我想在php中解码并使用base64编码的数组,我的代码如下:
I want to decode and use an base64 encoded array in php my code looks like this:
<?php
$hash = "YXJyYXkoInNhbXBsZV9pZCI9PiJObnJ5amZCV0p0STllalNRcnE2NHZIejFjc084RnVqUHRGRGk5WkdtM0Z3Vm9ieHprOSIsICJ4eF94eF94eHhfeHh4MSI9PiIwIiwgInNhbXBsZTIiID0+IjAiKQ==";
$hash_decoded = base64_decode($hash);
$all_infos = $hash_decoded;
$sample_id = $all_infos['sample_id'];
$xx_xx_xxx_xxx1 = $all_infos['xx_xx_xxx_xxx1'];
$sample2 = $all_infos['sample2'];
echo $sample_id; ?>
并且解码后的数组是
array("sample_id"=>"NnryjfBWJtI9ejSQrq64vHz1csO8FujPtFDi9ZGm3FwVobxzk9", "xx_xx_xxx_xxx1"=>"0", "sample2" =>"0")
我无法从阵列获取信息.控制台说
I can´t get the infos from the Array. The Console says
PHP Warning: Illegal string offset 'sample_id' in [...] on line 6
PHP Warning: Illegal string offset 'xx_xx_xxx_xxx1' in [...] on line 7
PHP Warning: Illegal string offset 'sample2' in [...] on line 8
a
问题出在哪里?感谢您的回答.
Where´s the problem? Thank´s for answers.
推荐答案
$all_infos
变量是一个字符串,因为这是您从base64_decode($hash)
返回的内容.然后,您不能期望它成为具有诸如sample_id
之类的属性的数组.
The $all_infos
variable is a string, since that is what you get back from base64_decode($hash)
. You cannot then expect it to become an array having properties like sample_id
.
此特定字符串具有PHP表达式编码,但是您需要解释该字符串.一种方法是使用臭名昭著的eval
函数.注意只有在您信任该字符串的来源时才使用它!
This particular string has a PHP expression encoded, but you'll need to interpret that string. One way is to use the infamous eval
function. Take care to only use it when you trust the source of that string!
eval('$all_infos = ' . $hash_decoded . ";");
这篇关于用PHP解码Base64编码的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!