ivor ivor - 2 months ago 10
JSON Question

How to Remove elements in array using php

is there an easy way to remove an element using php
like square brackets [] and commas ,

here is my sample code:

while($row = $result->fetch_assoc())
{
//$emparray['content'][] = $row;

$str = "<div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=".$row['contentid']."' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('".$row['thumbnail']."');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>".$row['contentname']."</h5><span class='uploadedTime'>".timeElapsedSinceNow($row['publishdate'])."</span></div><p class=''>".$row['desc_breif']."</p></div></div></a></div>";

$emparray['content'][] = $str;
$emparray['type'] = 'html';

}

//write to json file
$fp = fopen('rsspb.json', 'w');
//have a backslashes remover
fwrite($fp, json_encode($string, JSON_UNESCAPED_SLASHES));
fclose($fp);


and output is like this:

{"content":["<div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=00003478' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('http://dl.garenanow.com/games/pbph/pbphweb/webupdate/website/2016%20September/03-ic15gac/170x100.jpg');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>PBIC 2015 Gachapon</h5><span class='uploadedTime'>18 hours ago</span></div><p class=''>We are days away for the upcoming PointBlank International Championship 2015. This year PBIC 2015 will be held in Indonesia and 11 teams from different countrys will participate in this tournament</p></div></div></a></div>","<div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=00003475' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('http://dl.garenanow.com/games/pbph/pbphweb/webupdate/website/2016%20September/03-ic15pack/Website--Thumbnail.jpg');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>PBIC 2015 Weapon Package</h5><span class='uploadedTime'>1 day ago</span></div><p class=''>Get the limited edition Point Blank International Championship 2015 weapons and an additional itemto boost up your game, and enjoy up to 30% off for the original value of the package</p></div></div></a></div>","<div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=00003474' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('http://dl.garenanow.com/games/pbph/pbphweb/webupdate/website/2016%20September/03-ic15pack/Website--Thumbnail.jpg');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>Item Super Package</h5><span class='uploadedTime'>1 day ago</span></div><p class=''>Attention TroopersWe are days away for the upcoming PointBlank International Championship 2016. Showing the Garuda, The national symbol of indonesia. This represents a powerful warrior with speed and power, The official PBIC2015 weapons is a </p></div></div></a></div>","<div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=00003473' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('http://cdn.garenanow.com/community/esports/004-PointBlank/Articles/Cadet%20Cup%20Headers/Thumbnail.png');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>Cadet Cup - Occ Mindoro - Gamerz</h5><span class='uploadedTime'>1 day ago</span></div><p class=''>Now is your chance to tap the next level of eSports fever as Garena Philippines bring you another on ground tournament, the Cadet Cup. Cadet Cup heads to Occidental Mindoro this coming September 24, 2016! Form your teams now and check out how to register below to get a chance to take home cash prize, premium Point Blank in-game items, and limited edition Point Blank merchandise. Tournament starts at 1:00 PM!</p></div></div></a></div>","<div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=00003472' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('http://cdn.garenanow.com/community/esports/004-PointBlank/Articles/Cadet%20Cup%20Headers/Thumbnail.png');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>Cadet Cup - Zambales - Elite Gaming Zone</h5><span class='uploadedTime'>1 day ago</span></div><p class=''>Now is your chance to tap the next level of eSports fever as Garena Philippines bring you another on ground tournament, the</p></div></div></a></div>","<div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=00003471' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('http://dl.garenanow.com/games/pbph/pbphweb/webupdate/website/2016%20September/02-ic14set/Website--Thumbnail.jpg');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>PBIC 2014 Weapon Package</h5><span class='uploadedTime'>1 day ago</span></div><p class=''>Get the limited edition POintBlank International Championship 2014 weapons and an additional item or skin, and enjoy up to 30% off for the original value of the packageGet these limited edition PBIC 2014 Packages in </p></div></div></a></div>","<div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=00003470' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('http://dl.garenanow.com/games/pbph/pbphweb/webupdate/website/2016%20September/02-ic14/Website--Thumbnail.jpg');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>PBIC 2014 Weapon Collection</h5><span class='uploadedTime'>1 day ago</span></div><p class=''>Attention Troopers, PointBlank International Championship 2016 a week ahead. But before the fierce E-Sport event here is a look back of the competition that happened last 2014. Introducing the official weapon series that was used during </p></div></div></a></div>","<div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=00003469' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('http://dl.garenanow.com/games/pbph/pbphweb/webupdate/website/2014%20July/25/news.jpg');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>SG - Davao 9/24</h5><span class='uploadedTime'>1 day ago</span></div><p class=''>Here's your chance to prove your skills as Garena Philippines brings you another on ground tournament, the Point Blank Shooting Grounds. The tournament heads to Tarlac this coming September 24, 2016! Ready your squad now and get a chance to win a LARGE cash prize, in-game items, and limited edition Point Blank merchandise. find out how to register below!</p></div></div></a></div>","<div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=00003468' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('http://dl.garenanow.com/games/pbph/pbphweb/webupdate/website/2016%20September/00-kill/Website--Thumbnail.jpg');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>PBIC2016 Kill Challenge</h5><span class='uploadedTime'>1 day ago</span></div><p class=''>Attention Troopers!To Further Celebrate the Upcoming Point Blank International Championship 2016 here is another special event that will surely test your Stregth! Get the highest accumulated EXP from the PBIC EXP CHallenge and win yourself </p></div></div></a></div>","<div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=00003467' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('http://dl.garenanow.com/games/pbph/pbphweb/webupdate/website/2016%20September/00-PBIC-ultimate/Website--Thumbnail.jpg');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>PBIC2016 Ultimate Challenge</h5><span class='uploadedTime'>1 day ago</span></div><p class=''>Noong nagsimula kami sampung taon na ang nakaraan, hindi namin inakala kung ano ang mangyayari sa Riot at League of Legends. Sa kabutihang palad, naging matindi ang suporta ng ating player community na patuloy na lumalago isang dekada na ang nakalipas.</p></div></div></a></div>"],"type":"html"}


i want to make the out put like this:

{"content":"<div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=00003478' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('http://dl.garenanow.com/games/pbph/pbphweb/webupdate/website/2016%20September/03-ic15gac/170x100.jpg');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>PBIC 2015 Gachapon</h5><span class='uploadedTime'>18 hours ago</span></div><p class=''>We are days away for the upcoming PointBlank International Championship 2015. This year PBIC 2015 will be held in Indonesia and 11 teams from different countrys will participate in this tournament</p></div></div></a></div><div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=00003475' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('http://dl.garenanow.com/games/pbph/pbphweb/webupdate/website/2016%20September/03-ic15pack/Website--Thumbnail.jpg');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>PBIC 2015 Weapon Package</h5><span class='uploadedTime'>1 day ago</span></div><p class=''>Get the limited edition Point Blank International Championship 2015 weapons and an additional itemto boost up your game, and enjoy up to 30% off for the original value of the package</p></div></div></a></div><div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=00003474' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('http://dl.garenanow.com/games/pbph/pbphweb/webupdate/website/2016%20September/03-ic15pack/Website--Thumbnail.jpg');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>Item Super Package</h5><span class='uploadedTime'>1 day ago</span></div><p class=''>Attention TroopersWe are days away for the upcoming PointBlank International Championship 2016. Showing the Garuda, The national symbol of indonesia. This represents a powerful warrior with speed and power, The official PBIC2015 weapons is a </p></div></div></a></div><div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=00003473' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('http://cdn.garenanow.com/community/esports/004-PointBlank/Articles/Cadet%20Cup%20Headers/Thumbnail.png');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>Cadet Cup - Occ Mindoro - Gamerz</h5><span class='uploadedTime'>1 day ago</span></div><p class=''>Now is your chance to tap the next level of eSports fever as Garena Philippines bring you another on ground tournament, the Cadet Cup. Cadet Cup heads to Occidental Mindoro this coming September 24, 2016! Form your teams now and check out how to register below to get a chance to take home cash prize, premium Point Blank in-game items, and limited edition Point Blank merchandise. Tournament starts at 1:00 PM!</p></div></div></a></div><div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=00003472' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('http://cdn.garenanow.com/community/esports/004-PointBlank/Articles/Cadet%20Cup%20Headers/Thumbnail.png');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>Cadet Cup - Zambales - Elite Gaming Zone</h5><span class='uploadedTime'>1 day ago</span></div><p class=''>Now is your chance to tap the next level of eSports fever as Garena Philippines bring you another on ground tournament, the</p></div></div></a></div><div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=00003471' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('http://dl.garenanow.com/games/pbph/pbphweb/webupdate/website/2016%20September/02-ic14set/Website--Thumbnail.jpg');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>PBIC 2014 Weapon Package</h5><span class='uploadedTime'>1 day ago</span></div><p class=''>Get the limited edition POintBlank International Championship 2014 weapons and an additional item or skin, and enjoy up to 30% off for the original value of the packageGet these limited edition PBIC 2014 Packages in </p></div></div></a></div><div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=00003470' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('http://dl.garenanow.com/games/pbph/pbphweb/webupdate/website/2016%20September/02-ic14/Website--Thumbnail.jpg');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>PBIC 2014 Weapon Collection</h5><span class='uploadedTime'>1 day ago</span></div><p class=''>Attention Troopers, PointBlank International Championship 2016 a week ahead. But before the fierce E-Sport event here is a look back of the competition that happened last 2014. Introducing the official weapon series that was used during </p></div></div></a></div><div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=00003469' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('http://dl.garenanow.com/games/pbph/pbphweb/webupdate/website/2014%20July/25/news.jpg');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>SG - Davao 9/24</h5><span class='uploadedTime'>1 day ago</span></div><p class=''>Here's your chance to prove your skills as Garena Philippines brings you another on ground tournament, the Point Blank Shooting Grounds. The tournament heads to Tarlac this coming September 24, 2016! Ready your squad now and get a chance to win a LARGE cash prize, in-game items, and limited edition Point Blank merchandise. find out how to register below!</p></div></div></a></div><div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=00003468' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('http://dl.garenanow.com/games/pbph/pbphweb/webupdate/website/2016%20September/00-kill/Website--Thumbnail.jpg');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>PBIC2016 Kill Challenge</h5><span class='uploadedTime'>1 day ago</span></div><p class=''>Attention Troopers!To Further Celebrate the Upcoming Point Blank International Championship 2016 here is another special event that will surely test your Stregth! Get the highest accumulated EXP from the PBIC EXP CHallenge and win yourself </p></div></div></a></div><div class='row games-list'><a href='http://lolphnews.gxx.garena.com/pc_client_news.php?id=00003467' target='_blank'><div class='col-md-12'><div class='games-list-img' style='background-image: url('http://dl.garenanow.com/games/pbph/pbphweb/webupdate/website/2016%20September/00-PBIC-ultimate/Website--Thumbnail.jpg');'></div><div class='game-list-right'><div class='games-list-title'> <h5 class=''>PBIC2016 Ultimate Challenge</h5><span class='uploadedTime'>1 day ago</span></div><p class=''>Noong nagsimula kami sampung taon na ang nakaraan, hindi namin inakala kung ano ang mangyayari sa Riot at League of Legends. Sa kabutihang palad, naging matindi ang suporta ng ating player community na patuloy na lumalago isang dekada na ang nakalipas.</p></div></div></a></div>type":"html"}

Answer

The reason you get the square brackets in your JSON string, is because you've told PHP to save the data in a multi-dimentional array.

In other words, this line:

$emparray['content'][] = $str;

Is identical to this:

$emparray['content'] = array ($str);

This creates an array inside of the content index, filling it with the contents of the $str string. Making it a multi (2) dimensional array. Instead of just filling the index with the string itself.
Remove the extra set of square brackets, and you should find that your code works just the way you want it it.

Do a var_dump ($emparray) to see this for yourself.

Edit: As per your comment, you seem to also want to add more data to the content. This is where you have to make a choice: Whether you want all of the content as one string, as a whole page of HTML; Or if you want an array of strings, for different sections of a page. If it's the former, then you can just use string concatenation to add to the data in the index. For the latter, then you need to use an array and just deal with it on the client side.