我有一系列视频ID,当用户点击按钮时,点击视频的videoId会附加到数组中。
当用户再次点击同一按钮时,代码搜索videoId,如果找到,则从阵列中删除视频ID。
数组保存在json文件中,格式为["aaa","bob"...]
但是,当我从数组中删除一个值时,json文件将转换为{"1":"aaa", "2":"bbb"...}
如何防止这种情况发生?
PHP:
$NameFileJSON = $_GET["NameFile"];
$VideoId = $_GET["VideoId"];
$removeVideoId = $_GET["RemoveVideoId"];
$results = array($VideoId);
保存视频:
if (($NameFileJSON != "")&&($VideoId != "")&&($removeVideoId == "")){
$filename = "json/likesJSON/$NameFileJSON.json";
if (file_exists($filename)) {
echo "The file $filename exist";
$inp = file_get_contents("json/likesJSON/$NameFileJSON.json");
$arr = json_decode($inp);
array_push($arr, $results[0]);
$fp_login = fopen("json/$NameFileJSON.json", w);
fwrite($fp_login, json_encode($arr));
fclose($fp_login);
} else {
echo "The file $filename does not exist";
$fp_login = fopen("json/$NameFileJSON.json", w);
fwrite($fp_login, json_encode($results));
fclose($fp_login);
}
}
删除视频:
if (($NameFileJSON != "")&&($VideoId == "")&&($removeVideoId != "")){
$inp = file_get_contents("json/$NameFileJSON.json");
$arr = json_decode($inp);
if (($index = array_search($removeVideoId, $arr)) !== false) {
echo $index;
unset($arr[$index]);
}
$fp_login = fopen("json/$NameFileJSON.json", w);
fwrite($fp_login, json_encode($arr));
fclose($fp_login);
}
print_r(json_encode($arr)
答案 0 :(得分:0)
json_encode turns array into an object 使用json_encode标志强制它到数组
后确保它不是一个关联数组答案 1 :(得分:0)
json_encode
开始的后续数字, 0
将只生成数组表示法。当您使用unset()
时,它会在索引中创建一个间隙,因此它将作为对象发送以维护索引。因此,unset()
通常只能与关联数组一起使用。
使用array_splice()
而不是unset()
从数组中删除元素,然后将其后的所有索引向下移动。
array_splice($arr, $index, 1);
答案 2 :(得分:0)
同样的@Barry说,如果你仍然希望在json中保存为数组,你可以使用这个
<div class="copyright">
<p class="hidden-xs"> © Copyright </p>
</div>
<div class="navbar-fixed-bottom">
<div id="footerSlideContainer">
<div id="footerSlideButton"></div>
<div id="footerSlideContent">
<footer>
<div class="footer-01">
<div class="container">
<div class="row">
<br>
<h2 class="centered">THANKS FOR VISITING US</h2>
<hr>
<div class="col-lg-4">
<h3>Latest Tweets
</h3>
<div id="example1"></div>
<h4>Watch me on Periscope</h4>
<a href="#" class="periscope-on-air" data-size="large">@Periscope</a>
</div><!-- col -->
<div class="col-lg-4 border">
</div><!-- col -->
<div class="col-lg-4">
<h3> Subscribe </h3>
<p>Subscribe for the latest newsletters and updates</p>
<div id="mc_embed_signup" class="mailchimp">
<form action="..." method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate form-inline" target="_blank" novalidate>
<div class="form-group">
<input type="email" value="" name="EMAIL" class="required email form-control" id="mce-EMAIL" placeholder="Enter email">
<input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="btn btn__bottom--border mailchimp__btn" data-style="shrink" data-horizontal>
</div>
<div id="mce-responses" class="clear">
<div class="response" id="mce-error-response" style="display:none"></div>
<div class="response" id="mce-success-response" style="display:none"></div>
</div>
<div class="" style="position: absolute; left: -5000px;"><input type="text" name="..." value=""></div>
</form>
<span class="form_nospam">No spam</span>
</div><!--End mc_embed_signup-->
</div><!-- col -->
</div><!-- row -->
</div><!-- container -->
</div>
</footer>
<hr class="container">
<div class="container">
<a href="https://www.facebook.com/alwayssunny/?fref=ts" target="_blank"><i class="fa fa-facebook fa-2x"></i></a>
<a href="https://twitter.com/alwayssunny?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor" target="_blank"><i class="fa fa-twitter fa-2x"></i></a>
<a href="https://soundcloud.com/allisondanger/its-always-sunny-in-philadelphia-intro" target="_blank"><i class="fa fa-soundcloud fa-2x"></i></a>
<div class="pull-right">
<iframe width="100px" height="20" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/226657681&color=999999&auto_play=true&hide_related=false&show_comments=true&show_user=true&show_reposts=false"> </iframe>
</div>
</div>
</div>
</div>
</div>
</div>
了解详情
答案 3 :(得分:0)
unset
替换为array_splice
:array_splice($array, $i, 1);
array_values
重置索引:json_encode(array_values($array));