PHP - 如果值不正确,重复检索 XML
PHP - repeat retrieve XML if value is not correct
我在 PHP 中有一个代码可用于 XML:
$data_measuranment = file_get_contents($value, false, $context);
$xml=simplexml_load_string($data_measuranment);
$json = json_encode($xml);
$array[] = json_decode($json,TRUE);
我的数组($array[])的值取决于时间:
[1] => Array
(
[@attributes] => Array
(
[end] => 1520439534044
[start] => 1520439300000
[step] => 300000
)
[columns] => Array
(
[values] => Array
(
[0] => NaN
[1] => NaN
)
)
[timestamps] => Array
(
[0] => 1520439300000
[1] => 1520439600000
)
)
...
如果我几秒钟前执行代码。我的数组有效:
[1] => Array
(
[@attributes] => Array
(
[end] => 1520439565293
[start] => 1520439300000
[step] => 300000
)
[columns] => Array
(
[values] => Array
(
[0] => 12
[1] => NaN
)
)
[timestamps] => Array
(
[0] => 1520439300000
[1] => 1520439600000
)
)
...
唯一对我重要的值是数组[键][列][值][0]=12
所以,我需要一个代码来等待几秒钟,然后再次执行 API 的拉取,并在值数组 [1][列][值][0] 与 NaN 不同时完成。我试着用这段代码来做,但没有用:
do
{
foreach ($array as $valor)
{
$term= $valor['columns']['values']['0'] ;
if ($term === 'NaN')
{
unset($array);
sleep(10);
$data_measuranment = file_get_contents($value, false, $context);
$xml=simplexml_load_string($data_measuranment);
$json = json_encode($xml);
$array[] = json_decode($json,TRUE);
}
}
}while (isset($array))
我需要这个:当 foreach 读取值“$valor['columns']['values']['0']”并检查为 NaN 时。代码等待 10 秒并使用 file_get_contents 检索新数组,然后再次检查条件。如果 $valor['columns']['values']['0'] 的所有值都与 NaN 不同,则继续执行脚本。
谢谢。
让我们重申一下您的要求:
- 从远程服务器获取 XML。
- 如果
$valor['columns']['values']['0']
的 all 个值不等于 NaN
,则继续脚本。
- 否则,请等待 10 秒,然后重试。
您已正确识别出第 3 步需要一个 do ... while
循环才能回到开头,而第 2 步需要一个 foreach
循环来检查结果中的每个值。
问题是您在 foreach
循环中添加了太多逻辑,因此第 1 步和第 3 步发生在 第 2 步中。相反,您需要完整执行步骤2,然后决定是继续代码,还是休眠再试。
您可能会发现将代码分解为单独的函数很有帮助 - 第 2 步应该只是检查数据并返回它是 "good" 还是 "bad" 的指示,第 1 步应该只是获取数据并使用 SimpleXML:
解析它
# Variable to control do...while loop
$have_good_data = false;
do
{
# Step 1
$data_measuranment = file_get_contents($value, false, $context);
$xml = simplexml_load_string($data_measuranment);
# Step 2; this could be moved into a function that returns $have_nans
$have_nans = does_data_contain_nan($xml);
# Step 3
if ( $have_nans ) {
sleep(10);
} else {
$have_good_data = true;
}
} while ($have_good_data)
第 2 步的函数可能如下所示:
function does_data_contain_nan(\SimpleXMLElement $xml) {
foreach ($xml->children() as $valor)
{
$term = (string)$valor->columns->values[0];
if ($term === 'NaN')
{
// once we find a NaN, we don't need to look at the other values
return true;
}
}
// We got to the end of the loop without finding a NaN!
return false;
}
我在 PHP 中有一个代码可用于 XML:
$data_measuranment = file_get_contents($value, false, $context);
$xml=simplexml_load_string($data_measuranment);
$json = json_encode($xml);
$array[] = json_decode($json,TRUE);
我的数组($array[])的值取决于时间:
[1] => Array
(
[@attributes] => Array
(
[end] => 1520439534044
[start] => 1520439300000
[step] => 300000
)
[columns] => Array
(
[values] => Array
(
[0] => NaN
[1] => NaN
)
)
[timestamps] => Array
(
[0] => 1520439300000
[1] => 1520439600000
)
)
...
如果我几秒钟前执行代码。我的数组有效:
[1] => Array
(
[@attributes] => Array
(
[end] => 1520439565293
[start] => 1520439300000
[step] => 300000
)
[columns] => Array
(
[values] => Array
(
[0] => 12
[1] => NaN
)
)
[timestamps] => Array
(
[0] => 1520439300000
[1] => 1520439600000
)
)
...
唯一对我重要的值是数组[键][列][值][0]=12
所以,我需要一个代码来等待几秒钟,然后再次执行 API 的拉取,并在值数组 [1][列][值][0] 与 NaN 不同时完成。我试着用这段代码来做,但没有用:
do
{
foreach ($array as $valor)
{
$term= $valor['columns']['values']['0'] ;
if ($term === 'NaN')
{
unset($array);
sleep(10);
$data_measuranment = file_get_contents($value, false, $context);
$xml=simplexml_load_string($data_measuranment);
$json = json_encode($xml);
$array[] = json_decode($json,TRUE);
}
}
}while (isset($array))
我需要这个:当 foreach 读取值“$valor['columns']['values']['0']”并检查为 NaN 时。代码等待 10 秒并使用 file_get_contents 检索新数组,然后再次检查条件。如果 $valor['columns']['values']['0'] 的所有值都与 NaN 不同,则继续执行脚本。
谢谢。
让我们重申一下您的要求:
- 从远程服务器获取 XML。
- 如果
$valor['columns']['values']['0']
的 all 个值不等于NaN
,则继续脚本。 - 否则,请等待 10 秒,然后重试。
您已正确识别出第 3 步需要一个 do ... while
循环才能回到开头,而第 2 步需要一个 foreach
循环来检查结果中的每个值。
问题是您在 foreach
循环中添加了太多逻辑,因此第 1 步和第 3 步发生在 第 2 步中。相反,您需要完整执行步骤2,然后决定是继续代码,还是休眠再试。
您可能会发现将代码分解为单独的函数很有帮助 - 第 2 步应该只是检查数据并返回它是 "good" 还是 "bad" 的指示,第 1 步应该只是获取数据并使用 SimpleXML:
解析它# Variable to control do...while loop
$have_good_data = false;
do
{
# Step 1
$data_measuranment = file_get_contents($value, false, $context);
$xml = simplexml_load_string($data_measuranment);
# Step 2; this could be moved into a function that returns $have_nans
$have_nans = does_data_contain_nan($xml);
# Step 3
if ( $have_nans ) {
sleep(10);
} else {
$have_good_data = true;
}
} while ($have_good_data)
第 2 步的函数可能如下所示:
function does_data_contain_nan(\SimpleXMLElement $xml) {
foreach ($xml->children() as $valor)
{
$term = (string)$valor->columns->values[0];
if ($term === 'NaN')
{
// once we find a NaN, we don't need to look at the other values
return true;
}
}
// We got to the end of the loop without finding a NaN!
return false;
}