我试图在表格式打印每个数组值.但我有一个问题在这个代码


I am attempting to print an each array values in table format.But i having a trouble in this code

stdClass Object ( 
    [_total] => 2 
    [values] => Array ( 
        [0] => stdClass Object ( 
            [degree] => BCA 
            [endDate] => stdClass Object ( [year] => 2012 )
            [fieldOfStudy] => Computer Application 
            [id] => 170345308 [schoolName] => Tagore College of Arts and science 
            [startDate] => stdClass Object ( [year] => 2009 ) 
        ) 
        [1] => stdClass Object ( 
            [degree] => BCA 
            [endDate] => stdClass Object ( [year] => 2012 ) 
            [fieldOfStudy] => Computer Application 
            [id] => 170345431 
            [schoolName] => Tagore College of Arts and science 
            [startDate] => stdClass Object ( [year] => 2009 ) 
        ) 
     ) 
) 

我想打印上面代码的数组值。我使用print_r函数,它将打印所有的值。但我需要表格格式的个人值,如学校名称及其相应的值等。

<table>
        <tr>
        <th>schoolname</th>
        <th>field of study</th>
        <th>start date</th>
        <th>end date</th>
        <th>degree</th>
        <th>activites</th>
        </tr>
            <?php
            for($i=0;$i<$user->educations['total'];$i++)
            {   ?>
                <tr>
            <td><?php echo $user->educations['values'][$i]['schoolName'];?></td>
            <td><?php echo $user->educations['values'][$i]['degree'];?></td>
            <td><?php echo $user->educations['values'][$i]['fieldOfStudy'];?></td>
            <td><?php echo $user->educations['values'][$i]['startDate']['year'];?></td>
            <td><?php echo $user->educations['values'][$i]['endDate']['year'];?></td>
            </tr>
            <?php }?>
</table>

您的对象var_dump包含对象数组,但在您的代码中,您只是使用数组语法,这就是您的问题可能来自的地方。下面是一个如何处理这个问题的例子。

首先注意从使用for到使用foreach的变化。在遍历数组中的每个元素时使用foreach是一种很好的做法。它使代码更容易理解,并且删除了迭代器变量我美元。

其次注意如何

最后注意数组语法从$education['value']变为$education->value。

<table>
    <tr>
        <th>schoolname</th>
        <th>field of study</th>
        <th>start date</th>
        <th>end date</th>
        <th>degree</th>
        <th>activites</th>
    </tr>
    <?php foreach ($educations->values as $education) { ?>
    <tr>
        <td><?= $education->schoolName ?></td>
        <td><?= $education->degree ?></td>
        <td><?= $education->fieldOfStudy ?></td>
        <td><?= $education->startDate->year ?></td>
        <td><?= $education->endDate->year ?></td>
    </tr>
    <?php }?>
</table>

应该是;

$variable->FlightInfoResult->flights->originCity