清除数组索引

时间:2014-01-27 09:25:43

标签: php foreach indexing echo

如何清除数组索引。 因为如果我使用foreach我只得到[0]和[1] 或者我应该使用foreach代码吗?

** SQL Datenbank选择。 **

$data = $rs->fetchAllAssoc();

**我使用print_r ***时得到的数组

<pre><?php print_r ($this->data); ?></pre>


Array
    (
        [0] => Array
            (
                [id] => 1
                [pid] => 0
                [sorting] => 0
                [text] => text
            )

        [1] => Array
            (
                [id] => 2
                [pid] => 0
                [sorting] => 0
                [text] => text
            )
    )

**我用来获取数据 *

的PHP代码
        <?php foreach ($this->data as $datafield): ?>
            <td>
            <?php echo $datafield; ?>
            </td>
         <?php endforeach; ?>

* 在页面上我得到了这个* 数组数组

2 个答案:

答案 0 :(得分:0)

你只是在外部阵列中进行迭代。

内部应该有另一个循环。

<?php foreach ($this->data as $outerIndex => $array): ?>
    <?php foreach($array as $innerIndex => $data): ?>
        <td>
            <?php echo $innerIndex; ?>
        </td>
    <?php endforeach; ?>
<?php endforeach; ?>

答案 1 :(得分:0)

如果你是print_r($ datafield),这就是你将得到的:

Array
            (
                [id] => 1
                [pid] => 0
                [sorting] => 0
                [text] => text
            )

Array
            (
                [id] => 2
                [pid] => 0
                [sorting] => 0
                [text] => text
            )

这意味着您没有迭代内部数组,即$ this-&gt; data [0]和$ this-&gt; data [1]

所以你应该有另一个内部foreach循环遍历$ datafield。

<?php foreach ($this->data as $datafield): ?>
         <?php foreach($datafield as $key => $value): ?>
            <td>
            <?php echo $value; ?>
            </td>
          <?php endforeach; ?>
<?php endforeach; ?>

那会给你:

1
0
0
text
2
0
0
text