duanhuo7441 2016-05-26 09:23
浏览 24

在PHP代码中无法掌握功能概念

So I have an index.php file that has data.php (array list of different types of items):

$catalog[304] = [
    "title" => "The Very Thought of You",
    "img" => "img/media/nat_king_cole.jpg",
    "genre" => "Jaz",
    "format" => "MP3",
    "year" => 2008,
    "category" => "Music",
    "artist" => "Nat King Cole"
]; //and so on ->

I also have a function.php file that hold this function:

function get_item_html($id, $item) {
    $output =
        "<li><a href='#'><img src='"
        . $item["img"] . "' alt='"
        . $item["title"] . "' />"
        . "<p> View Details </p>"
        . "</a></li>";
    return $output;

And then the index.php which have both files included at the top:

<ul class="items">
$random = array_rand($catalog, 4);
foreach ($random as $id) {
echo get_item_html($id, $catalog[$id]);
}

What I can't grasp is the "$catalog[$id]". It seems like it should return the catalog ID, not the key within the catalog ID. But apparently the code is valid, because it does work. It's just that I can't understand why.

I know that I am way off in this but if someone could explain it to me I would be super grateful.

Thanks!

  • 写回答

1条回答 默认 最新

  • douzhi6160 2016-05-26 09:43
    关注

    $catalog[$id] simply returns an array with id=$id from your data.php file and your get_item_html method utilises img and title values of this array and generates some html. So if your random generator picked lets say 304 then your output would be:

    <li><a href='#'><img src='img/media/nat_king_cole.jpg' alt='The Very Thought of You' />
          <p> View Details</p>
        </a>
    </li>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类