Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

robinvlaar's avatar

php laravel array foreach

json data information example:

pair : btcusdt

period (seconds) : 14400, 180, 1800, 21600, 259200, 300, 3600, 43200, 60, 604800, 7200, 86400, 900

Normally (on the site that provides data)

-btcusdt array items count = 4864 (all periods total)

-ltcusdt array items count = 4861 (all periods total)

-neousdt array items count = 4860 (all periods total)

but when you run the code in the dump screen

-btcusdt array items count = 4864

-ltcusdt array items count = 9724 (include btc array items)

-neousdt array items count = 14585 (include btc and ltc array items)

The code running process is taking too long when the number of pair increases...

What I do make mistakes ?

    public function CwCoinIndicators3()
    {
        ini_set('max_execution_time', 36000);
        ini_set('memory_limit', '-1');
        ini_set('trader.real_precision', '8');
        $url1 = "https://api.cryptowat.ch/markets/binance";
        $json1 = collect(json_decode(file_get_contents($url1), true));
        $data1 = $json1->all();
        $no = 0;
        foreach ($data1['result'] as $key => $value) {
        $no = $no + 1;            
            if ($value['pair'] == 'btcusdt' || $value['pair'] == 'ltcusdt' || $value['pair'] == 'neousdt') {
        echo $no . " - " . $value['pair'] . "</br>";            
        $url2 = "https://api.cryptowat.ch/markets/binance/". $value['pair'] . "/ohlc";
        $json2 = collect(json_decode(file_get_contents($url2), true));
        $data2 = $json2->all();
        echo $url2 . "</br>";
        $dataperiod = array_keys($data2['result']);
        dump($data2);
        foreach ($dataperiod as $key1 => $value1) {
            if (is_array($data2['result'][$value1]) || is_object($data2['result'][$value1]))
            {        
               foreach ($data2['result'][$value1] as $key2 => $value2) 
               {
                $open = array_column($data2['result'][$value1], 1);
                $high = array_column($data2['result'][$value1], 2);
                $low = array_column($data2['result'][$value1], 3);
                $close = array_column($data2['result'][$value1], 4);
                $rsi14 = trader_rsi($close,14);
                }
            }
        $c1 = count($data2['result'][$value1]);
        for ($i=0; $i < $c1; $i++) { 
            $arr[] = array_merge (
                //$data2['result'][$value1][$i], 
                array(
                    'Opentime' => $data2['result'][$value1][$i][0],
                    'Open' => sprintf('%.8f', floatval($open[$i])),
                    'High' => sprintf('%.8f', floatval($high[$i])),
                    'Low' => sprintf('%.8f', floatval($low[$i])),
                    'Close' => sprintf('%.8f', floatval($close[$i])),
                    'OpenDate' => date('Y-m-d H:i:s',$data2['result'][$value1][$i][0]),
                    'Rsi14' => isset($rsi14[$i]) ? $rsi14[$i] : null,
                    'pair' => $value['pair'],
                    'Per' => $value1,
                ));
                }
            }
            dump($arr);
            //echo "<pre>" . print_r($arr) . "</pre>";
        foreach ($arr as $key => $arr2) {
        $exist = CwIndicators::where(['pair'=> $arr2['pair'], 'Per' => $arr2['Per'] ,'Opentime'=> $arr2['Opentime']])->get();
            if(count($exist)  > 0) {

                CwIndicators::where('pair', $arr2['pair'])
                                  ->where('Per', $arr2['Per'])
                                  ->where('Opentime', $arr2['Opentime'])
                                  ->update([
                                    'OpenDate' => date('Y-m-d H:i:s', $arr2['Opentime']),
                                    'Open' => $arr2['Open'],
                                    'High' => $arr2['High'],
                                    'Low' => $arr2['Low'],
                                    'Close' => $arr2['Close'],
                                    'Rsi14' => $arr2['Rsi14'],
                                    ]); 
            }
            else  {
                CwIndicators::updateOrCreate(['pair' => $arr2['pair'],
                                    'Per' => $arr2['Per'],
                                    'Opentime'=> $arr2['Opentime'],
                                    'OpenDate' => date('Y-m-d H:i:s', $arr2['Opentime']),
                                    'Open' => $arr2['Open'],
                                    'High' => $arr2['High'],
                                    'Low' => $arr2['Low'],
                                    'Close' => $arr2['Close'],
                                    'Rsi14' => $arr2['Rsi14'],
                                    ]); 
                
                    } 
                }               
            }     
        }
    }
0 likes
2 replies
topvillas's avatar

Is there a question or problem in there somewhere?

Please or to participate in this conversation.