如何从多维数组中仅打印一个元素? (我不知道该数组的布局方式)

时间:2019-01-28 03:16:59

标签: php arrays multidimensional-array

我有一个多维数组,目前尚无法在心理上进行评估,并且想知道如何从数组中仅打印一个元素,例如打印$ cities [“ City” [0] [0] [0] ...(我不知道要得到一个元素,应该有多少[...]

<?php 




$cities = array( array("City"=>"New York", "State"=> "NY", "Population"=>8175133),
               array("City"=>"Los Angeles", "State"=> "CA" , "Population"=>3792621),
               array("City"=>"Chicago", "State"=> "IL" , "Population"=>2695598),
               array("City"=>"Houston", "State"=> "TX" , "Population"=>2100263),
               array("City"=>"Philadelphia", "State"=> "PA" , "Population"=>1526006),
               array("City"=>"Phoenix", "State"=> "AZ" , "Population"=>1445632),
               array("City"=>"San Antonio", "State"=> "TX" , "Population"=>1327407),
               array("City"=>"San Diego", "State"=> "CA" , "Population"=>1307402),
               array("City"=>"Dallas", "State"=> "TX" , "Population"=>1197816),
           array("City"=>"San Jose","State" => "CA", "Population"=>945942)  
             ); 


//what I want to do (very simple)
print $cities["City"][0][0] //....????


/*
?>

<?php if (count($cities) > 0): ?>
<table>
  <thead>
    <tr>
      <th><?php echo implode('</th><th>', array_keys(current($cities))); ?></th>
    </tr>
  </thead>
  <tbody>
<?php foreach ($cities as $row): array_map('htmlentities', $row); ?>
    <tr>
      <td><?php echo implode('</td><td>', $row); ?></td>
    </tr>
<?php endforeach; ?>
  </tbody>
</table>
<?php endif; ?>



*/

我只想能够打印出此数组的元素。

2 个答案:

答案 0 :(得分:0)

echo $x[0]['City'];将打印出“纽约”

这是$ city [ index ] [ key ]

答案 1 :(得分:0)

打印出来的数组如下:

Array
(
    [0] => Array
        (
            [City] => New York
            [State] => NY
            [Population] => 8175133
        )

    [1] => Array
        (
            [City] => Los Angeles
            [State] => CA
            [Population] => 3792621
        )

    [2] => Array
        (
            [City] => Chicago
            [State] => IL
            [Population] => 2695598
        )

    [3] => Array
        (
            [City] => Houston
            [State] => TX
            [Population] => 2100263
        )

    [4] => Array
        (
            [City] => Philadelphia
            [State] => PA
            [Population] => 1526006
        )

    [5] => Array
        (
            [City] => Phoenix
            [State] => AZ
            [Population] => 1445632
        )

    [6] => Array
        (
            [City] => San Antonio
            [State] => TX
            [Population] => 1327407
        )

    [7] => Array
        (
            [City] => San Diego
            [State] => CA
            [Population] => 1307402
        )

    [8] => Array
        (
            [City] => Dallas
            [State] => TX
            [Population] => 1197816
        )

    [9] => Array
        (
            [City] => San Jose
            [State] => CA
            [Population] => 945942
        )

这意味着您有一个包含10个元素的数组。

每个元素都包含它自己的数组,该数组由三个元素组成,分别代表城市,州和人口。

要访问5个元素的城市值,请执行以下操作:

echo $cities[4]['City']; //<---Philadelphia

数组从0开始计数(索引)。因此第5个元素实际上在#4处索引。

希望有帮助!