Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用按键作为指示器?_Php_Html_Arrays - Fatal编程技术网

Php 如何使用按键作为指示器?

Php 如何使用按键作为指示器?,php,html,arrays,Php,Html,Arrays,我正在探索数组以及如何使用它 我遇到了这个不寻常的阵列 我正在尝试使用我在下面发布的数组来实现这个输出 我怎样才能完成这项工作? <table> <tr> <th>DOGS</th> <th>CATS</th> <th>BIRDS</th> </tr> <tr> <td><?php echo $

我正在探索数组以及如何使用它

我遇到了这个不寻常的阵列

我正在尝试使用我在下面发布的数组来实现这个输出

我怎样才能完成这项工作?

<table>
   <tr>
      <th>DOGS</th>
      <th>CATS</th>
      <th>BIRDS</th>
   </tr>
   <tr>
      <td><?php echo $dogs;?></td>
      <td><?php echo $cats;?></td>
      <td><?php echo $birds;?></td>
   </tr>
</table>

Array
(
    [cats] => Array
        (
            [0] => persian
            [1] => ragdoll
            [2] => siberian
            [3] => savannah
        )

    [dogs] => Array
        (
            [0] => husky
            [1] => bulldog
            [2] => beagle
            [3] => labrador
        )

    [birds] => Array
        (
            [0] => parrot
            [1] => owl
            [2] => eagle
            [3] => love birds
        )
)

您需要首先循环它们,不能直接回送阵列:

$animals = array(
    'dogs' => array('husky', 'bulldog', 'beagle', 'labrador'),
    'cats' => array('persian', 'ragdoll', 'siberian', 'savannah'),
    'birds' => array('parrot', 'owl', 'eagle', 'love birds'),
);

?>

<table cellpadding="10">
<tr><th>DOGS</th><th>CATS</th><th>BIRDS</th></tr>
<?php
$types = array_keys($animals);
for($i = 0; $i < count($animals['cats']); $i++) {
    echo '<tr>';
    foreach($types as $type) {
        echo '<td>' . $animals[$type][$i] . '</td>';
    }
    echo '</tr>';
}
?>
</table>
$animals=数组(
'dogs'=>数组('husky','bulldog','beagle','labrador'),
'猫'=>数组('波斯','碎布娃娃','西伯利亚','萨凡纳'),
'birds'=>数组('parrot'、'owl'、'eagle'、'love birds'),
);
?>
狗粪鸟

@Arif_suhail_123是的,我只是想这样,先把钥匙拿出来,这样你就可以把它们排成一行了。是的,你只是想这样,我正试图解决这个问题。由于我对数组不太在行,所以我拼命的自我,比我看到有人贴了答案。所以我多次检查了你的示例输出链接,我在想这到底是如何工作的,然后我明白了。哦,是的,这个人很聪明。你是说,想想看。酷毙了。@Arif_suhail_123基本上,第一个for循环决定了它将循环多少行,第二个循环(foreach)基本上是负责列的循环,因为你必须把它们排成一行(狗、猫、鸟,从数组键),谢谢你的回答,我在阅读了你给出的链接很多次之后才明白(英语不是我的第一语言希望我不会把你弄糊涂).我知道这可能是一个很大的问题。你能推荐一些书或教程吗?特别是在玩数组方面。如果你不回答就可以了。目前我正在检查你给出的一些答案,学到了很多东西。我必须说你是天才。谢谢你的回答,你是对的。祝你有愉快的一天。
$animals = array(
    'dogs' => array('husky', 'bulldog', 'beagle', 'labrador'),
    'cats' => array('persian', 'ragdoll', 'siberian', 'savannah'),
    'birds' => array('parrot', 'owl', 'eagle', 'love birds'),
);

?>

<table cellpadding="10">
<tr><th>DOGS</th><th>CATS</th><th>BIRDS</th></tr>
<?php
$types = array_keys($animals);
for($i = 0; $i < count($animals['cats']); $i++) {
    echo '<tr>';
    foreach($types as $type) {
        echo '<td>' . $animals[$type][$i] . '</td>';
    }
    echo '</tr>';
}
?>
</table>