douza1373 2013-09-12 21:24
浏览 28
已采纳

echo中的PHP数组

I have an assignment to do but am having trouble understanding the given psuedocode :/

<?php
$bucket = new array();
print $bucket->addrocks('Rock1')->addrocks('rock2')-
>addrocks('Rock3');

echo "<h1>My Bucket</h1>";
echo "<ul>";
foreach($bucket as $rock){
echo "<li>". $rock ."</li>";
}
echo "</ul>";
?>

Now my trouble starts with understanding how they want me to construct the array after the "print" call? i am not looking for a direct answer to this just maybe a tutorial link or a finger in the right direction

Thank you in advance

  • 写回答

3条回答 默认 最新

  • dongliao8069 2013-09-12 21:30
    关注

    In PHP, new is only used for instantiating objects Furthermore, array is a reserved word in PHP, so name your class something else. To instantiate an array in PHP you do this:

     $my_array = array();
    

    Now to add items to the array you would do this:

     $my_array[] = "Rock 1";
     $my_array[] = "Rock 2";
     $my_array[] = "Rock 3";
    

    To traverse the array you can use any type of loop, but usually you would just use a foreach loop.

    For example:

     foreach($my_array as $key => $value) {
       echo $value . "<br />";
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应