Jump to content
werushka

PHP loop removing last comma

Recommended Posts

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

Share this post


Link to post
Share on other sites

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}

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Non of the above, I am trying to solve my problem which is indicated in my first post. Nothing more nothing less. 

Share this post


Link to post
Share on other sites

Instead of building a string, build an array and then implode() it with commas. Or use substr() to get rid of the last character.

  • Like 1

Share this post


Link to post
Share on other sites

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.

Edited by Jacques1

Share this post


Link to post
Share on other sites
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 
Edited by werushka

Share this post


Link to post
Share on other sites

The part you're worried about looks like it is. Make an array and then encode it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.