Cannot output multi-dimensional array

2019-07-04 21:18发布

问题:

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

I would like to get my output as:

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

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.

Current output: (when echoing $message)

  • 1
  • 2
  • 3
  • 4

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>

Here is my full code:

<?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 } ?>

Function where arrays are defined

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;
        }

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" } [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

回答1:

The problem is with how you use array_push here:

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

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.

Do the following instead:

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

or just

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

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

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