如何在循环外使用php变量
如何在循环外保存输出结果
此变量$ jsonrs
这是我想要循环外的结果
“ 1”“ 1.jpg”“ 2”“ 2.jpg”“ 3”“ 3.jpg”“ 4”“ 4.jpg”
$url = 'https://hentaifox.com/gallery/58769/';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
preg_match_all('!<img class="lazy no_image" data-src="(.*?)"!', $result, $manga_name);
$items = array();
foreach ($manga_name[1] as $key => $manganm) {
$imag_manga = str_replace('t.jpg','.jpg',$manganm);
$imagerep = 'https:'.$imag_manga;
$filename = basename($imagerep);
$imag_num = str_replace('.jpg','',$filename);
$array_name = array($imag_num => $filename);
$json1 = json_encode($imag_num);
$json2 = json_encode($filename);
$jsonrs = $json1.$json2;
print_r($jsonrs);
}
答案 0 :(得分:0)
您在$jsonrs
中得到的最终结果有点不寻常,我(我假设)要对图像列表进行JSON编码,如果这样,则使用$items
保留每个图像的列表,然后然后在循环后json_encode()
将此列表...
foreach ($manga_name[1] as $key => $manganm) {
$filename = basename($manganm);
$imag_num = str_replace('t.jpg','',$filename);
$items[$imag_num] = $filename;
}
echo json_encode($items);
会给你
{"1":"1t.jpg","2":"2t.jpg","3":"3t.jpg","4":"4t.jpg"}