遇到阵列问题

时间:2014-03-03 21:04:17

标签: php

我有一个查询返回具有ID,坐标(X,Y)和特定ID数组的行。我想要做的是获取特定ID的坐标并将它们传递给新数组。它适用于我的数组中的第一个项目,但不会拉动后续项目。

以下代码:

$AllPoints = $pdo->prepare($sqlAllPoints);
    $AllPoints->execute();
    $wpArray = array();
    $i = 1;
    while($apRow = $AllPoints->fetch(PDO::FETCH_ASSOC)) //WHY TF IS THIS NOT WORKING PAST 1??
    {
        for($i = 1; $apRow['UID'] == $optRoute[$i]; $i++)
        {
            echo("<br/>UID: " . $apRow['UID'] . " | UID2: " . $optRoute[$i] . "<br/>");
            $wpCoor = $apRow['Y'] . ", " . $apRow['X'];
            array_push($wpArray,$wpCoor);
        }
    }

    echo("<br/> wpArray: ");
    print_r($wpArray);
    echo("<br/>" . $i);

然后它返回:

  

数组([0] =&gt;原点[1] =&gt; 47293 [2] =&gt; 36586 [3] =&gt; 60426 [4] =&gt;   48488 [5] =&gt; Dest)UID:47293 | UID2:47293

     

wpArray:数组([0] =&gt; 33.735064,-97.143939)1

(它应该忽略Origin和Dest点,我只想要数组中的航点)

非常感谢任何帮助。 谢谢!

更新 我改变了我的循环(黑客攻击大杂烩)让它发挥作用...我确定有更好的方法......

for($i=0; $i <= (count($apRow) + 1); $i++)
        {
            if ($apRow['UID'] == $optRoute[$i])
            {
                echo("<br/>UID: " . $apRow['UID'] . " | UID2: " . $optRoute[$i] . "<br/>");
                $wpCoor = $apRow['Y'] . ", " . $apRow['X'];
                $wpArray[] = $wpCoor;
            }
        }

返回:

  

数组([0] =&gt;原点[1] =&gt; 47293 [2] =&gt; 36586 [3] =&gt; 60426 [4] =&gt;   48488 [5] =&gt; Dest)
  UID:36586 | UID2:36586

     

UID:47293 | UID2:47293

     

UID:48488 | UID2:48488

     

UID:60426 | UID2:60426

     

wpArray:Array([0] =&gt; 35.3214,-94.4414 [1] =&gt; 33.735064,-97.143939   [2] =&gt; 38.1404534,-85.737987 [3] =&gt; 38.8604916,-90.1018053)

2 个答案:

答案 0 :(得分:0)

    <?php
        $AllPoints = $pdo->prepare($sqlAllPoints);
        $AllPoints->execute();
        $wpArray = array();
        $i = 1;
        $apData = $AllPoints->fetch(PDO::FETCH_ASSOC)
        foreach ($apData as $apRow)
        {
            foreach($optRoute as $optR)
            {
                if ($optR == $apRow['UID']){
                    echo("<br/>UID: " . $apRow['UID'] . " | UID2: " . $optR . "<br/>");
                    $wpCoor = $apRow['Y'] . ", " . $apRow['X'];
                    array_push($wpArray,$wpCoor);
                }
            }
        }

        echo("<br/> wpArray: ");
        print_r($wpArray);
        echo("<br/>" . $i);
    ?>

答案 1 :(得分:0)

您的for循环表达式(循环停止条件)始终为TRUE,这就是循环仅在一行上进行交互的原因。

$apRow['UID'] == $optRoute[$i]
// from YOUR output: UID: 47293 | UID2: 47293
// TRUE == TRUE

=&GT;停止循环。