【问题标题】:Nested Key-Value lookups in phpphp中的嵌套键值查找
【发布时间】:2012-02-09 18:06:18
【问题描述】:

我正在尝试为学校的一个班级制作一个“股票”网站,这是我第一次深入研究 php。基本上,该脚本从谷歌文档电子表格中提取一个 CSV 文件,并(尝试)将这些值放入一个数组中以供以后使用。我想展示前 5 名上涨和下跌的股票,但我遇到了问题。这是脚本的主要部分:

<html>
 <head>
   <?php
     #Global Variables
     $rising = array();
     $falling = array();
     $stocks = array();
     #End Global Variables

     #Function to read data from the spreadsheet
     function get_data($url){
        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
     }

     #Process data
     function populateTicker(){
        $document = "https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0AtrtT_MC9_YFdHRDUGx0a2xveXNfOHJVdXJ6bVNkMFE&output=csv";
        $data= get_data($document);
        $lines = explode("\n", $data);
        $val = "";
        foreach($lines as $key => $value){
            if($key != 0){
                $stockInfo = explode(",", $value);
                $perChange = $stockInfo[3];
                $perChangeVal = "up ";
                if($perChange < 0){
                    $perChangeVal = "down ";
                    $falling['$stockInfo[0]'] = $perChange;
                }else{
                    $rising['$stockInfo[0]'] = $perChange;
                }
                $stocks['$stockInfo[0]'] = array("symb" => $stockInfo[0], "name" => $stockInfo[1], "price" => $stockInfo[2]);
                $val = $val . "(" . $stockInfo [0] . ")  " . $stockInfo [1]  . "  " . "\$" . $stockInfo [2] . " " . $perChangeVal .  $perChange . "% today" . "\v \v \v \v | \v \v \v \v";
            }
        }
        //asort($falling);
        //arsort($rising);
        return $val;
     }

     function getRising($index){
        if($index <= count($rising)){
            $keys = array_keys($rising);
            $data = $stocks[$keys[$index]];
            return "(" . $data['symb'] . ")  " . $data['name'] . "  " . "\$" . $data['price'];
        }else{
            return ".";
        }

     }

     function getFalling($index){
        if($index <= count($falling)){
            $keys = array_keys($falling);
            $data = $stocks[$keys[$index]];
            return "(" . $data['symb'] . ")  " . $data['name'] . "  " . "\$" . $data['price'];
        }else{
            return ".";
        }
     }
   ?>
 </head>
 <body>
   <DIV id='DEBUG'>
        <?php
            print_r($stocks);
            print_r($rising);
            print_r($falling);
        ?>
   </DIV>
  <center><b><u><font size="+2">Latest Prices</font><br /></u></b></center>
  <DIV ID="TICKER" STYLE="border-top:2px solid #CCCCCC; border-bottom:2px solid #CCCCCC; overflow:hidden; width:100%" onmouseover="TICKER_PAUSED=true" onmouseout="TICKER_PAUSED=false">
    <?php echo populateTicker(); ?>
  </DIV>
  <script type="text/javascript" src="webticker_lib.js" language="javascript"></script>
  <div id='Top5'>
   <br />
   <center><b>This page does not update automatically! Please refresh the page to update the information!</b></center>
   <br />
   <center><b><u><font size="+2">Top 5's</font><br /></u></b></center>
   <center>
       <table border="1" cellpadding="5">
          <tr>
             <th>Top 5 Rising</th>
             <th>Top 5 Falling</th>
          </tr>
          <tr>
             <td><?php echo getRising(1); ?></td>
             <td><?php echo getFalling(1); ?></td>
          </tr>
          <tr>
             <td><?php echo getRising(2); ?></td>
             <td><?php echo getFalling(2); ?></td>
          </tr>      <tr>
             <td><?php echo getRising(3); ?></td>
             <td><?php echo getFalling(3); ?></td>
          </tr>      <tr>
             <td><?php echo getRising(4); ?></td>
             <td><?php echo getFalling(4); ?></td>
          </tr>      <tr>
             <td><?php echo getRising(5); ?></td>
             <td><?php echo getFalling(5); ?></td>
          </tr>
       </table>
    </center>
  </div>
  <br />
  <center><b><u><font size="+2">All Stocks</font><br /></u></b></center>
  <div id='All'>
    <center>
        <table border="1" cellpadding="5">
          <tr>
             <th>Symbol</th>
             <th>Name</th>
             <th>Price</th>
             <th>High</th>
             <th>Low</th>
             <th>Percent Change</th>
          </tr>
          <?php
            #Dynamic Table Creation
            foreach($stocks as $key => $value){
                echo '<tr>';
                    echo '<td>(' . $value['symb'] . ')</td>';
                    echo '<td>' . $value['name'] . '</td>';
                    echo '<td>' . $value['price'] . '</td>';
                    echo '<td></td>';
                    echo '<td></td>';
                    echo '<td>' . $vaule['perChange'] . '</td>';
                echo '</tr>';
            }
          ?>
        </table>
    </center>
  </div>
 </body>
 <footer>
 </footer>
</html>

但是没有任何东西被分配给数组。任何帮助将不胜感激。

更新:我添加了首页的完整源代码 index.php UPDATE2:我想通了。我来自java,并没有完全理解变量的范围是如何在php中工作的。一个简单的

    <?php
global $rising, $falling, $stocks;
...
?>

成功了

【问题讨论】:

  • 您似乎没有在任何地方调用您的函数。顺便说一句,如果这是“家庭作业”,您应该将其标记为这样,以便人们知道。
  • 不是功课,在别处调用了函数,给您造成的困扰,见谅!我知道它正在执行,因为 populateTicker() 函数返回预期数据。我遇到的问题是 $rising 和 $falling 的数组分配似乎没有做任何事情,因为数组保持空白......
  • 欢迎来到 SO btw。很高兴你知道我只是想建议!我建议你花一些时间让步进调试器工作,另外你可能想看看单元测试。我假设您对这些来自 Java 的东西有经验。您的代码中还有一个错误: $rising['$stockInfo[0]'] 应该只是 $rising[$stockInfo[0]] 否则它会将 '$stockInfo[0]' 视为字符串文字。

标签: php arrays web stocks array-push


【解决方案1】:

我不完全了解您的代码,但我可以展示一个展示嵌套数组的示例:

$arr = array('1' => array('1', '2'), '2');

function showNested($array)
{
  foreach($array as $key => $value)
  {
    if(is_array($value))
    {
      echo $value;
      showNested($array);
    }
    else
    {
      echo $value;
    }
  }
}

更新

您在代码中使用了$stocks['$stockInfo[0]']。我认为这种语法永远不会做任何事情。完全当你在字符串中使用变量时,你应该用{}包围它。还有一件事是我以前从未测试过的,我认为在字符串中放置一个带有索引的数组不会帮助 PHP 理解 [] 中的当前数据。

【讨论】:

  • 对不起,我一定不是很清楚(这是我第一次发帖)。我的问题不是如何读取数组,而是如何存储它?因为 array_push 和直接赋值似乎都没有做任何事情......
  • 我听不懂。你说的商店是什么意思?您想读取每个字段并将其存储在另一个数组中吗?如果是,请使用$arr[] = $data。我认为最好使用老式方式,因为使用 array_push 会对数组进行一些操作,并且您应该在每次使用后重置您的数组。我在某处读过。
猜你喜欢
  • 1970-01-01
  • 2019-03-02
  • 2018-01-30
  • 2020-02-16
  • 1970-01-01
  • 1970-01-01
  • 2019-01-19
  • 1970-01-01
  • 2017-08-02
相关资源
最近更新 更多