我的一个项目的模板是一个三维数组,就需要遍历输出,这实际上使用foreach就可以很快实现
if ($_GET['on'] == 'on'){
new_json($template,'./data/m.json');
echo json_encode($template);
} else {//下为数组遍历输出
#echo '<plaintext>';
$data_1 = read_json('./data/m.json');
#print_r ($data_1);
foreach($data_1 as $v){
foreach($v as $v_1){
foreach($v_1 as $v_2){
echo $v_2;
}
}
}
}
array:
Array
(
[0] => Array
(
[0] => Array
(
[1] => <nav>
[2] => <a href="
[3] => http://www.ecy.ink
[4] => " class="active" target="_blank">
[5] => 十织的博客
[6] => </a>
[7] => <a class="clip"></a>
[8] => </nav>
)
[1] => Array
(
[1] => <div class="background"><canvas id="startrack" width="1798" height="1413"></canvas><div class="cover"></div></div>
)
[2] => Array
(
[1] => <div class="ch intro"><div class="container">
[2] => <div class="hello">
[3] => <h1 id="slogan">
[4] => 每个人<br>都有一个世界<br>安静而孤独</h1><!-- 随机文字 page.js -->
[5] => <div class="kong"></div><span class="toppic-line"></span>
[6] => <h2><div class="circle"><span></span><!-- 红点 --><span></span><!-- 黄点 --><span></span><!-- 绿点 --></div>
[7] => 标题:Hello stars, hello world!
[8] => </h2></div></div></div>
)
.....
使用js把插入HTML插入到页面当中
<script src="./style_script/text.js"></script>
<script>
let text = load("http://127.0.0.1/hshw/api/m.php?on=");
document.write(text);
console.log(text);
</script>
./style_script/text.js:
function load(name) {
let xhr = new XMLHttpRequest(),
okStatus = document.location.protocol === "file:" ? 0 : 200;
xhr.open('GET', name, false);
xhr.overrideMimeType("text/html;charset=utf-8");//默认为utf-8
xhr.send(null);
return xhr.status === okStatus ? xhr.responseText : null;
}
不过我前端不太好,目前也开始努力学了