使用带有json_decode的php中的Base64解码json字符串

使用带有json_decode的php中的Base64解码json字符串

问题描述:

I'm trying to decode output json data using json_decode & Base64 decode, no issue for output json_decode but one string that have base64 encode I also no issue to decode it using base64_decode,. but I'm try to decode both json data using json_decode & base64_decode that i face some problems, My question is now how to decode the JSON string on same time one string use base64 decode... here an example of my json data

Array
(
 [0] => Array
    (
        [ad_id] => 257
        [stat] => 1
        [text] => 2YTZhNio2YrYuSDYtNmB2LEg2YPYp9io2KrZitmB2YrYpyDZhdmI2K/ZitmEINmi2aDZoNmp
        [img_count] => 4
        [img_file_name] => 8246ee83.jpg
    )
[1] => Array
    (
        [ad_id] => 258
        [stat] => 1
        [text] => 2YTZhNio2YrYuSDYtNmB2LEg2KfZhCDYqtmKINiy2K8g2YXZiNiv2YrZhCDZotmg2aDZpQ==
        [img_count] => 5
        [img_file_name] => 1563457.jpg
    )

string 'text' as output working ..but with limit array [0] << one output only if i replace $output[0] with $output[1] i got output of array 1 like that ,,i need to be dynamic [0] or [1] or [2] ..etc base on output.. ,,and print the other strings with json_decode.

<?
$ch = curl_init("http://www.example.com/s=0&c=20");
curl_setopt( $ch, CURLOPT_USERAGENT, 'Dalvik/2.1.0 (Linux; U; Android 5.0;  SM-N9005 Build/LRX21V)' );
curl_setopt($ch,CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip'));
$result = curl_exec($ch);
$json = json_decode( $result, TRUE);
$output[0]['text']=base64_decode($json[0]['text']);
header('Content-Type: text/plain; charset=utf-8');

#print_r ($json);

print_r ($output);

?>

thanks

我正在尝试使用json_decode&amp;解码输出json数据。 Base64解码,输出json_decode没问题,但是一个有base64编码的字符串我也没问题用base64_decode解码它。 但我尝试使用json_decode&amp;解码两个json数据 base64_decode,我遇到了一些问题,我的问题是现在如何解码JSON字符串同时一个字符串使用base64解码...这里是我的json数据的一个例子 p>

 数组 
(
 [0] =&gt;数组
(
 [ad_id] =&gt; 257 
 [stat] =&gt; 1 
 [text] =&gt; 2YTZhNio2YrYuSDYtNmB2LEg2YPYp9io2KrZitmB2YrYpyDZhdmI2K / ZitmEINmi2aDZoNmp 
 [img_count] =  &gt; 4 
 [img_file_name] =&gt; 8246ee83.jpg 
)
 [1] =&gt;数组
(
 [ad_id] =&gt; 258 
 [stat] =&gt; 1 
 [  text] =&gt; 2YTZhNio2YrYuSDYtNmB2LEg2KfZhCDYqtmKINiy2K8g2YXZiNiv2YrZhCDZotmg2aDZpQ == 
 [img_count] =&gt; 5 
 [img_file_name] =&gt; 1563457.jpg 
)
  code>  pre> 
 
 

string' text'作为输出工作..但带限制数组[0]&lt;&lt;一个输出只有当我用$ output [1]替换$ output [0]我得到了数组1的输出,我需要是动态的 [0]或[1]或[2] .etc基于输出.. ,,并使用json_decode打印其他字符串。 p>

 &lt;?
 $ ch = curl_init(“http://www.example.com/s=0&c=20”); 
curl_setopt($ ch,CURLOPT_USERAGENT,'Dalvik / 2.1.0(Linux;  U;  Android 5.0;  SM-N9005 Build / LRX21V)'); 
curl_setopt($ ch,CURLOPT_ENCODING,''); 
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ ch,CURLOPT_HTTPHEADER,array('Accept-Encoding:gzip')  )); 
 $ result = curl_exec($ ch); 
 $ json = json_decode($ result,TRUE); 
 $ output [0] ['text'] = base64_decode($ json [0] ['text  ']); 
header('Content-Type:text / plain; charset = utf-8'); 
 
#print_r($ json); 
 
print_r($ output); 
 
?&gt;  ; 
  code>  pre> 
 
 

谢谢 p> div>

I understand, do this:

$json = json_decode($result, true);
foreach($json as $k => $v){ // this will loop through all the possible indexes
    $output[$k]['text'] = base64_decode($v['text']);
}
header('Content-Type: text/plain; charset=utf-8');
print_r($output); // here's your new PHP array with all the ['text'] values!