如何正确处理foreach循环以及从数组中增加动态内容

时间:2018-01-05 08:30:36

标签: php arrays json

我是初学者编码器,我不断陷入简单的阵列和输出处理。

我一直坐在这里一天没有试图在这里张贴但是沮丧让我大声笑,我试图打印输出的所有algos。我将解释:

$w = file_get_contents(utf8_encode($nna->nice_url.$ns->method1.$cur_addy)) 

$w变量从网址https://nicehash.com/api?method=stats.provider&addr=BTCaddyhere中提取信息。然后,它将具有类似的输出。

{  
   "result":{  
      "stats":[  
         {  
            "balance":"0.00036893",
            "rejected_speed":"0",
            "algo":5,
            "accepted_speed":"0"
         },
         {  
            "balance":"0.00000033",
            "rejected_speed":"0",
            "algo":7,
            "accepted_speed":"0"
         },
      ],
      "payments":[  

      ],
      "addr":"14PvYoNEGttZt2JBZV9mHdr"
   },
   "method":"stats.provider"
}

现在这个输出是动态的,好像我只使用Lyra2re2挖掘它只会在算法下显示数字14。一旦盈利能力发生变化,它将通过将其添加到输出中来反映并显示其利润。

我试图只显示方法1'

输出的可用数字
foreach ($na->algo as $k => $v) {
    print $ss['result']['stats'][$k++]['algo']."<br />";
    print $ss['result']['stats'][$k++]['balance']."<br />";
}

我已经尝试增加100%工作的偏移并显示每个算法收集的正确数字,虽然它会显示正确的算法,但会创建未定义的变量,因为它无法找到输出以匹配算法数组所以它将数字输出为未定义的变量。

基本上我想为统计数据创建一个表,并在api的输出发生变化时自动递增,并将其他算法添加到列表中,并显示accepted_speed rejected_speed等等。我不确定这个数组是否是将数字分配给字符串的有效方法?

Bellow你会找到我的3个php页面:1.index.php,2.class.inc.php,3.functions.inc.php

只是一个想法,将json数据放入MySQL数据库,并让它更新信息,只需使用PHP显示该数据库中的什么?只有可以实时更新数据库

任何帮助将不胜感激:)。 谢谢

&#13;
&#13;
//index.php page below 

<!DOCTYPE html>
<?php
//files required
require_once('config.inc.php');
require_once('class.inc.php');
require_once('functions.inc.php');
?>
<html>
	<head>
		<title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
	</head>
	<body>
		<div id="stats_provider">
			<form action="" method="POST">
				<label>BTC ADDR: </label>
				<input type="text" name="btc_addr" style="min-width: 32px; width: 290px; padding-left: 3px;" placeholder="EG: 14i97joNEGttZt2JBZLU7HdrjxevFukjNG7" />
				<button>Enter</button>
			</form>
		</div>
		<?php
			
				$n = get_btc_addy();
				if (isset($cur_addy)) {
					$ns = new methods();
					$nna = new nice_api();
					$w = file_get_contents(utf8_encode($nna->nice_url.$ns->method1.$cur_addy));
					$ss = json_decode($w, true);
					$na = new algos();
					foreach ($na->algo as $k => $v) {
					    print $ss['result']['stats'][$k++]['algo']."<br />";
					    print $ss['result']['stats'][$k++]['balance']."<br />";
					}


				}
			
		?>
		<div id="current_api_version">
			<?php
				$ver = get_api_version();
				print "<p>Current API Version: ".$l."</p>";
			?>
		</div>
		
	</body>
</html>
&#13;
&#13;
&#13;

&#13;
&#13;
//Class.inc.php page

<?php
class nice_api {
	public $api_url = 'https://nicehash.com/api';
	public $nice_url = 'https://nicehash.com/';
}

class methods {

	public $method1 = 'api?method=stats.provider&addr=';
	public $method1x = 'api?method=stats.provider.ex&';
	public $method2 = 'api?method=stats.global.current&';
	public $method3 = 'api?method=stats.global.24h&';
	public $method4 = 'api?method=stats.provider.workers&addr=';
	public $method5 = 'api?method=multialgo.info';
}

class algos {
	public $algo = array(
		0 => 'Scrypt',
		1 => 'SHA256',
		2 => 'ScryptNf',
		3 => 'X11',
		4 => 'X13',
		5 => 'Keccak',
		6 => 'X15',
		7 => 'Nist5',
		8 => 'NeoScrypt',
		9 => 'Lyra2RE',
		10 => 'WhirlpoolX',
		11 => 'Qubit',
		12 => 'Quark',
		13 => 'Axiom',
		14 => 'Lyra2REv2',
		15 => 'ScryptJaneNf16',
		16 => 'Blake256r8',
		17 => 'Blake256r14',
		18 => 'Blake256r8vnl',
		19 => 'Hodl',
		20 => 'DaggerHashimoto',
		21 => 'Decred',
		22 => 'Cryptonite',
		23 => 'Lbry',
		24 => 'Equihash',
		25 => 'Pascal',
		26 => 'X11 Ghost',
		27 => 'Sia',
		28 => 'Blake2s',
		29 => 'Skunk'
	);
}

?>
&#13;
&#13;
&#13;

&#13;
&#13;
//functions.inc.php

<?php
require_once('class.inc.php');

function get_api_version() {
	$url = new nice_api();
	$api_version = file_get_contents(utf8_encode($url->api_url));
	$api_ver = json_decode($api_version, true);
    global $l; 
    $l = $api_ver['result']['api_version'];
}

function get_btc_addy() {
	//dynamically change addy for each user
	if (isset($_POST['btc_addr'])) {
				$ibtc = $_POST['btc_addr'];
				global $cur_addy;
				$cur_addy = $ibtc;
			
	}	
}
?>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

尝试更改此循环:

foreach ($na->algo as $k => $v) {
    print $ss['result']['stats'][$k++]['algo']."<br />";
    print $ss['result']['stats'][$k++]['balance']."<br />";
}

通过这种方式:

foreach ($ss['result']['status'] as $s){
    print $na->algo[$s['algo']];
}

答案 1 :(得分:0)

foreach我不确定它是什么。
如果你想循环杰森,你会喜欢这样:

$w ='{  
"result":{  
  "stats":[  
     {  
        "balance":"0.00036893",
        "rejected_speed":"0",
        "algo":5,
        "accepted_speed":"0"
     },
     {  
        "balance":"0.00000033",
        "rejected_speed":"0",
        "algo":7,
        "accepted_speed":"0"
     },
     {  
        "balance":"0.00283658",
        "rejected_speed":"0",
        "algo":14,
        "accepted_speed":"0"
     },
     {  
        "balance":"0.00000186",
        "rejected_speed":"0",
        "algo":20,
        "accepted_speed":"0"
     },
     {  
        "balance":"0.00000003",
        "rejected_speed":"0",
        "algo":21,
        "accepted_speed":"0"
     },
     {  
        "balance":"0.0027542",
        "rejected_speed":"0",
        "algo":22,
        "accepted_speed":"0.00000067"
     },
     {  
        "balance":"0.00002058",
        "rejected_speed":"0",
        "algo":23,
        "accepted_speed":"0"
     },
     {  
        "balance":"0.00029013",
        "rejected_speed":"0",
        "algo":24,
        "accepted_speed":"0"
     },
     {  
        "balance":"0.00000006",
        "rejected_speed":"0",
        "algo":25,
        "accepted_speed":"0"
     },
     {  
        "balance":"0.00000001",
        "rejected_speed":"0",
        "algo":26,
        "accepted_speed":"0"
     },
     {  
        "balance":"0.00000039",
        "rejected_speed":"0",
        "algo":27,
        "accepted_speed":"0"
     },
     {  
        "balance":"0.00069389",
        "rejected_speed":"0",
        "algo":28,
        "accepted_speed":"0"
     }
  ],
  "payments":[  

  ],
  "addr":"14PvYoNEGttZt2JBZV9mHdr"
},
"method":"stats.provider"
}';

$ss = json_decode($w, true);
$na = new algos();
foreach ($ss['result']['stats'] as $k => $v) {
    print $na[$v['algo']]."<br />\n";
    print $v['balance']."<br />\n\n";
}

foreach $ss['result']['stats']表示$v = $ss['result']['stats'] 那样$v['algo']输出id(或者它是什么)。

https://3v4l.org/OLjUl

为了您自己,请使用真实的变量名称 使用$w$ss$n及其所有内容,这个混乱将立即失控。