dongzhui2636 2010-10-12 19:15
浏览 22
已采纳

PHP Api - 结果/数组问题

I have been looking around for PHP tutorials and I found a very detailed one that have been useful to me.

But now, I have a question. The results showed by the API are stored into a $results array. This is the code (for instance):

$fetch = mysql_fetch_row($go);
$return = Array($fetch[0],$fetch[1]);    
$results = Array(  
    'news' => Array (  
        'id'    => $return[0],  
        'title' => $return[1]  
    )); 

My question is.. I want to display the last 10 news.. how do I do this? On normal PHP / mySQL it can be done as:

    while($var = mysql_fetch_array($table)) {
    echo "do something"
 }

How can I make it so the $results can print multiple results?

I have tried like:

while($var = mysql_fetch_array($table)) {
    $results = Array(  
        'news' => Array (  
            'id'    => $return[0],  
            'title' => $return[1]  
        )); 
 }

But this only shows me one result. If I change the $results .= Array(...) it gives error.

What can I do?

Thanks!

Edit

My function to read it doesn't read when I put it the suggested way:

function write(XMLWriter $xml, $data){
                            foreach($data as $key => $value){
                                    if(is_array($value)){
                                            $xml->startElement($key);
                                            write($xml, $value);
                                            $xml->endElement();
                                            continue;
                                    }
                                    $xml->writeElement($key, $value);
                            }
                    }
                    write($xml, $results);
  • 写回答

2条回答 默认 最新

  • dousuitang5239 2010-10-12 19:19
    关注
    $results[] = Array(  
            'news' => Array (  
                'id'    => $return[0],  
                'title' => $return[1]  
            )); 
    

    That should do it.

    If you are familiar with arrays, this is the equivalent of an array_push();

    array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed. Has the same effect as:

    <?php
    $array[] = $var;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能