无法输出多维数组 [英] Cannot output multi-dimensional array

查看:104
本文介绍了无法输出多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从多维数组中获取任何输出。我想使用push_array方法获取我的数据。

I cannot get any output from my multi-dimensional array. I'm trying to get my data using the push_array method.

我想得到我的输出:


  • John,1001

  • Tom,1002

  • Jerry,1003

  • Sarah,1004

  • John, 1001
  • Tom, 1002
  • Jerry, 1003
  • Sarah, 1004

但是,我没有得到任何输出。我知道数组正在工作,当我回显$ message并且不在我的输出foreach数组中使用$ id和$ name时,我得到以下输出。

However, I'm not getting any output. I know the array is working though as I get the following output when I echo $message and do not use $id and $name in my output foreach array.

当前输出:(当回显$ message时)

Current output: (when echoing $message)


  • 1

  • 2

  • 3

  • 4

  • 1
  • 2
  • 3
  • 4

但是当我使用$ id和$ name这里显示的时候没有显示。

But nothing gets shown when I use $id and $name as shown here.

<ul class="result"><?php
    foreach ($output as $message) {
        $id = $message[1][0];
        $name = $message[1][1];
    ?><li>
        <a href="handleDownload.php?id=<?php echo $id;?>"><?php echo $name; ?>      </a>
    </li><php } ?>
</ul>

这是我的完整代码:

<?php 
$Download = new Download();
$result = array(); //store results of upload
try {
    $Download->showDBFiles();
    $output = $Download->getMessages();
} catch (Exception $e) {
    $result[] = $e->getMessage();
}

?>

<h2>Output</h2>
<?php if ($output) { ?>
    <ul class="result">
    <?php  foreach ($output as $message) {
        $id = $message[1][0];
        $name = $message[1][1];
    ?>
        <li><a href="handleDownload.php?id=<?php echo $id;?>"><?php echo $name; ?></a></li>
<?php } ?>
</ul>
<?php } ?>

定义数组的功能

protected $messages = array();

        public function showDBFiles() {
            global $database;
            $values=array();
            $sql = "SELECT resume_id, resume_title FROM ".self::$table_name;
            $result = $database->query($sql);
            if(mysqli_num_rows($result)==0){
                 $this->messages[] = "Database is empty";
            }else{
                while(list($id, $name) = mysqli_fetch_array($result)){
                    array_push($this->messages, array($id, $name));
                }
            }
        }

        public function getMessages()
        {
                return $this->messages;
        }

更新:
使用jedrzej.kurylo的建议后,我可以拥有$ id和$ name值,如我的var_dump输出所示:

UPDATE: After using the suggestion by jedrzej.kurylo I'm able to have the $id and $name values as shown in my var_dump output:

array(4){[0] => array(2){[0 ] => string(2)73[1] => string(2)qd} [1] => array(2){[0] => string(2)72[1] => string(3)jav} [2] => array(2){[0] => string(2)70[1] => string(4)da12 2){[0] => string(2)71[1] => string(3)jav}}

array(4) { [0]=> array(2) { [0]=> string(2) "73" [1]=> string(2) "qd" } [1]=> array(2) { [0]=> string(2) "72" [1]=> string(3) "jav" } [2]=> array(2) { [0]=> string(2) "70" [1]=> string(4) "da12" } [3]=> array(2) { [0]=> string(2) "71" [1]=> string(3) "jav" } }

现在我不确定我的输出是这样的:

However for some reason that I'm unsure of my output is now this:


  • d

  • a

  • a

  • a

  • d
  • a
  • a
  • a

推荐答案

p>问题在于如何在这里使用 array_push

The problem is with how you use array_push here:

$this->messages[] = array_push($values, array($id, $name));

array_push 不返回更新的数组,但新的数量项阵列 - 请参阅 http://php.net/manual/en/function.array- push.php 。这就是为什么你的输出中整数增加。

array_push does not return updated array but the new number of items in the array - see http://php.net/manual/en/function.array-push.php. That's why you're getting increasing integers in your output.

请改为:

while(list($id, $name) = mysqli_fetch_array($result)){
  array_push($this->messages, array($id, $name));
}

或只是

while(list($id, $name) = mysqli_fetch_array($result)){
  $this->messages[] = array('id'=>$id, 'name'=>$name);
}

然后,您可以访问 $ id $ name 如下:

Then you can access $id and $name like this:

$id = $message['id'];
$name = $message['name'];

这篇关于无法输出多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆