Jump to content

PHP loop removing last comma


werushka

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

Link to comment
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
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.