如何迭代对象和数组项
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何迭代对象和数组项相关的知识,希望对你有一定的参考价值。
我不知道如何迭代这个输出,它可以很简单,但尝试几种方法没有成功。
我有一个查询的结果,抛出一个带有对象和修复的json,但我无法获得cpu,内存和磁盘值:
<?php
// How to read influx data with curl
$query = urlencode("select * from calls limit 1");
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 1); //timeout in seconds
curl_setopt($ch, CURLOPT_URL,"http://127.0.0.1:8086/query?db=myDB&q=$query");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$res = json_decode(curl_exec ($ch));
curl_close ($ch);
var_dump($res);
?>
我得到这个输出:
{"results":[{"statement_id":0,"series":[{"name":"custom","tags":{"hostname":"LINUX01"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",1,78,0,0]]},{"name":"custom","tags":{"hostname":"LINUX02"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",6,48,0,0]]},{"name":"custom","tags":{"hostname":"LINUX03"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",5,47,0,0]]},{"name":"custom","tags":{"hostname":"LINUX04"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",35,94,0,0]]},{"name":"custom","tags":{"hostname":"LINUX05"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",3,47,0,0]]},{"name":"custom","tags":{"hostname":"LINUX06"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",57,25,0,0]]},{"name":"custom","tags":{"hostname":"LINUX07"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",40,47,0,0]]},{"name":"custom","tags":{"hostname":"LINUX08"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",41,30,0,0]]},{"name":"custom","tags":{"hostname":"ues90078"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",40,20,0,0]]},{"name":"custom","tags":{"hostname":"LINUX09"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",33,45,0,0]]},{"name":"custom","tags":{"hostname":"LINUX041"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",31,30,0,0]]},{"name":"custom","tags":{"hostname":"LINUX042"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",33,8,0,0]]},{"name":"custom","tags":{"hostname":"LINUX043"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",54,10,0,0]]}]}]}
试试例子:
foreach ($res as $test) {
echo $test->statement_id->cpu;
}
我希望我能得到这个:
hostname time cpu mem load load
linux01 1970-01-01T00:00:00Z 1 78 0 0
对不起,我的英语不好。
答案
您将很难处理这些数据,因为大多数数据都是未加密的,这使得它作为对象使用起来会产生误导。如果使用数组,在这种情况下会更容易。您可以使用true作为解码的第二个参数来完成此操作。
$data = json_decode(curl_exec ($ch), true);
$results = $data['results'];
foreach ($results['series'] as $server) {
$cpuIndex = array_search('cpu', $server['columns']);
echo $server['values'][$cpuIndex];
}
另一答案
您需要将其转换为JSON到Object或Array。
这是一个常见的Array情况:
$results = json_decode('{"results":[{"statement_id":0,"series":[{"name":"custom","tags":{"hostname":"LINUX01"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",1,78,0,0]]},{"name":"custom","tags":{"hostname":"LINUX02"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",6,48,0,0]]},{"name":"custom","tags":{"hostname":"LINUX03"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",5,47,0,0]]},{"name":"custom","tags":{"hostname":"LINUX04"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",35,94,0,0]]},{"name":"custom","tags":{"hostname":"LINUX05"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",3,47,0,0]]},{"name":"custom","tags":{"hostname":"LINUX06"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",57,25,0,0]]},{"name":"custom","tags":{"hostname":"LINUX07"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",40,47,0,0]]},{"name":"custom","tags":{"hostname":"LINUX08"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",41,30,0,0]]},{"name":"custom","tags":{"hostname":"ues90078"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",40,20,0,0]]},{"name":"custom","tags":{"hostname":"LINUX09"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",33,45,0,0]]},{"name":"custom","tags":{"hostname":"LINUX041"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",31,30,0,0]]},{"name":"custom","tags":{"hostname":"LINUX042"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",33,8,0,0]]},{"name":"custom","tags":{"hostname":"LINUX043"},"columns":["time","cpu","mem","load","load"],"values":[["1970-01-01T00:00:00Z",54,10,0,0]]}]}]}', true);
foreach ($results["results"][0]["series"] as $array) {
echo "<table>";
echo "<thead>";
echo "<tr>";
foreach ($array["columns"] as $value) {
echo "<th>{$value}</th>";
}
echo "</tr>";
echo "</thead>";
echo "<tbody>";
echo "<tr>";
foreach ($array["values"][0] as $value) {
echo "<td>{$value}</td>";
}
echo "</tr>";
echo "</tbody>";
echo "<table>";
}
如果你想要特定的东西:
foreach ($results["results"][0]["series"] as $array) {
foreach ($array["columns"] as $key => $value) {
if ($value === "cpu") $column = $key;
}
echo "cpu: {$array["values"][0][$column]}.<br>";
}
要添加主机名,请添加:
echo "<tr>";
echo "<th colspan=" . count($array["columns"]) . ">{$array["tags"]["hostname"]}</th>";
echo "</tr>";
在echo "<thead>";
之后。
以上是关于如何迭代对象和数组项的主要内容,如果未能解决你的问题,请参考以下文章