werushka Posted September 22, 2017 Share Posted September 22, 2017 Hi, I have been trying to solve it but no luck, I have the following code <?php foreach ($tickers as $i => $ticker) { { echo '{ "description": "", "proName": "BITTREX:'. $ticker .'" },'; } } ?> it gives me output like below { "description": "", "proName": "BITTREX:PTCBTC" },{ "description": "", "proName": "BITTREX:NEOSBTC" }, but I need to remove the last comma that is generated. I would appreciate some feedback. Thanks Link to comment Share on other sites More sharing options...
Jacques1 Posted September 22, 2017 Share Posted September 22, 2017 Looks like you're trying to generate JSON documents by hand. Don't do that. PHP is perfectly capable of doing that itself -- properly. Link to comment Share on other sites More sharing options...
werushka Posted September 22, 2017 Author Share Posted September 22, 2017 Thanks for your reply but, I am totally not doing that. I have a cvs file which I get ticker data and trying to implement it to widget {script} Link to comment Share on other sites More sharing options...
Jacques1 Posted September 22, 2017 Share Posted September 22, 2017 Thanks for your reply but, I am totally not doing that. Do you have a reason for that, or do you just like to waste your time on useless, buggy code? Because if it's the latter, it looks like you've already achieved your goal. Link to comment Share on other sites More sharing options...
werushka Posted September 22, 2017 Author Share Posted September 22, 2017 Non of the above, I am trying to solve my problem which is indicated in my first post. Nothing more nothing less. Link to comment Share on other sites More sharing options...
Sepodati Posted September 22, 2017 Share Posted September 22, 2017 Instead of building a string, build an array and then implode() it with commas. Or use substr() to get rid of the last character. Link to comment Share on other sites More sharing options...
Jacques1 Posted September 22, 2017 Share Posted September 22, 2017 Guys. I understand that PHP people love to turn simple problems into fugly hacks with lots of bugs, but let's not be ridiculous. This is JSON. PHP knows JSON. Turning an array into a JSON document takes one function call. One. No loops, no string fumbling, no comma gymnastics. One function call. Writing this down is a matter of, say, 30 seconds including a quick check of the manual. Instead, you've already spent an hour banging your head against the wall. Even as a non-programmer, you should be able to see that this is not very smart. Link to comment Share on other sites More sharing options...
werushka Posted September 22, 2017 Author Share Posted September 22, 2017 is this json??? <!-- TradingView Widget BEGIN --> <span id="tradingview-copyright"><a ref="nofollow noopener" target="_blank" href="http://www.tradingview.com" style="color: rgb(173, 174, 176); font-family: "Trebuchet MS", Tahoma, Arial, sans-serif; font-size: 13px;">Quotes by <span style="color: #3BB3E4">TradingView</span></a></span> <script src="https://s3.tradingvi...t-tickers.js">{ "symbols": [ { "description": "", "proName": "BITTREX:ARKBTC" }, { "description": "", "proName": "BITTREX:OMGBTC" } ], "locale": "en" }</script> <!-- TradingView Widget END --> What I am trying to do is get the tickers from cvs file so I don't waste space on the php file. Typing it manually for each ticker. { "description": "", "proName": "BITTREX:OMGBTC" } I am trying to change "BITTREX:OMGBTC" with the data pulled from cvs file for each line Link to comment Share on other sites More sharing options...
Sepodati Posted September 22, 2017 Share Posted September 22, 2017 The part you're worried about looks like it is. Make an array and then encode it. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.