PHP - 如果值不正确,重复检索XML

时间:2018-03-07 20:28:32

标签: php arrays foreach simplexml do-while

我在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
                )

        )
...

我唯一重要的值是array [key] [columns] [values] [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不同,则继续执行脚本。

谢谢。

1 个答案:

答案 0 :(得分:0)

让我们重申您的要求:

  1. 从远程服务器获取XML。
  2. 如果$valor['columns']['values']['0']所有值不等于NaN,请继续执行该脚本。
  3. 否则,请等待10秒再试一次。
  4. 您已正确地将步骤3确定为需要do ... while循环返回到开头,而步骤2则需要foreach循环来检查结果中的每个值。

    问题是您在foreach循环中添加了太多逻辑,因此第1步和第3步发生在第2步。相反,您需要执行完整的第2步,然后决定是继续使用代码,还是睡觉并重试。

    您可能会发现将代码分解为单独的函数很有帮助 - 第2步应该只是检查数据并返回指示它是否是好的"或者"坏",第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;
    }
    
相关问题