【问题标题】:Object error when trying to print index 0 on array with PHP尝试使用 PHP 在数组上打印索引 0 时出现对象错误
【发布时间】:2014-02-01 06:04:38
【问题描述】:

我是 PHP 新手,遇到错误:

"Fatal error: Cannot use object of type Confrence as array on line 22"

我正在尝试将放入数组中的数据放入表中,其中第 1 列第 1 行中有 1 号种子,第 2 列第 1 行中有 16 号种子。我不知道这是否正确这样做的逻辑,但这是我的目标。由于某种原因,它不会回显数组的第一个索引。这是我的代码。

Class Conference:
<?php

    class Confrence
    {
        public $team1;
        public $team2;

        function loadGame($teamone, $teamtwo)
        {
            $this->$team1 = $teamone;
            $this->$team2 = $teamtwo;
        }

    }

?>


This is my main code.

    <?php print( '<?xml version = "1.0" encoding = "utf-8"?>' ) ?> 
    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>User Selection Page</title>
    </head>

    <?php

        require_once("loadGameClass") or die ("Could not load file");

        $westTeams = array();

        $loadGameClass = new Confrence();

        $loadGameClass->loadGame("(1) Gonzaga", "(16) Southern U");
        $westTeams = $loadGameClass;
        echo $westTeams[0];


    ?>

    <body>



    </body>
    </html>

【问题讨论】:

  • Confrence 类是否实现了ArrayAccess
  • 您的对象正在通过 loadGame() 设置 team1 和 team2,然后您将对象分配到数组变量中,这将永远无法通过在分配给数组变量之前将对象转换为 (数组) $loadGameClass;
  • 你能打印出你想要的结果吗?

标签: php


【解决方案1】:

$westTeams$loadGameClass 的变量引用了您的 Conference 对象,该对象不是数组。因此,您不能使用 $westTeams[0] 这样的符号。

如果您计划在班级中加入许多团队,您可能需要向 Conference 班级提交一个数组并将其存储起来。

班级会议:

class Conference
{
    protected $teams;

    function loadTeams($teams)
    {
        $this->teams = $teams;
    }

    function getTeams()
    {
        return $this->teams;
    }
}

关于将这些放在表格中,您可以使用以下代码。我假设您总共有 16 个团队。

<table>
    <tbody>
<?php
    /* Load your teams in the array */
    $loadGameClass = new Conference();
    $loadGameClass->loadTeams(array(...));
    $teams = $loadGameClass->getTeams();
    for ($i = 0; $i < 8; $i++) {
        $highSeed = $teams[$i];
        $lowSeed = $teams[((2*8)-1)-$i];
        echo "<tr><td>$highSeed</td><td>$lowSeed</td></tr>";
    }
?>
    </tbody>
</table>

这将输出第一列第一行的第一个种子和第二列第一行的第 16 个种子。该模式将重复到第 8 列。

在我的代码中,会议类有点用处,因为它只是团队数组的容器,但您可以向该类添加功能以使其有价值。

【讨论】:

  • 非常感谢!这非常有效!我在在线 php 代码测试仪上对其进行了测试,但输出没有出现在表格中。但是,它订购了 (1)(16),并在它打印的每个元素之间插入了一个中断。
  • 是的,我不应该为每个高低种子对开始一个新行。我已经编辑了答案
  • 再次感谢您对 ginovva320 的帮助。我修复了因为边框没有设置为 1 而看不到表格的问题。
【解决方案2】:

loadGame 方法没有返回任何内容,因此在其上使用 echo 不会有任何作用。

您看到的错误是由于 [x] 语法仅适用于数组,您正在使用一个类。

如果类成员变量是公开的,因此您可以在 html 中使用以下内容:

$westTeams-&gt;team1;

但是你还需要改类,你有语法错误:

$this-&gt;$team1 应该是$this-&gt;team1

【讨论】:

  • 获得一个团队并将其放入数组中,然后在表格中填充该数组的最佳方法是什么?
  • 表是什么意思...mysql表还是html表?
  • 一个 html 表格。下面的人输出正确,但没有打印在表格中。
【解决方案3】:

代替

$westTeams[0]

使用

$westTeams->team1

也用

$this->team1

而不是

$this->$team1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2016-09-01
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多