多维的城市阵列

时间:2015-07-01 20:39:34

标签: php arrays

我有多个ID(0,1,2)。如何从以下数组中获取相应ID的城市名称?

这是一个示例数组:

$cities_array = array (
  0 => 
  array (
    'city' => 'Sant Julià de Lòria',
    'region' => '06',
    'country' => 'AD',
    'latitude' => '42.46372',
    'longitude' => '1.49129',
  ),
  1 => 
  array (
    'city' => 'Pas de la Casa',
    'region' => '03',
    'country' => 'AD',
    'latitude' => '42.54277',
    'longitude' => '1.73361',
  ),
  2 => 
  array (
    'city' => 'Ordino',
    'region' => '05',
    'country' => 'AD',
    'latitude' => '42.55623',
    'longitude' => '1.53319',
  )
);

3 个答案:

答案 0 :(得分:0)

所以你需要使用以下代码访问php关联数组

 foreach($cities_array as $key => $value)
{
    echo $value['city'].' '.$value['region'].' '.$value['country'].' '.$value['latitude'].' '.$value['longitude']."<br>";
}

如果您没有在浏览器中显示,请使用以下代码

foreach($cities_array as $key => $value)
{
    echo $value['city'].' '.$value['region'].' '.$value['country'].' '.$value['latitude'].' '.$value['longitude']."\r\n";
}

如果您搜索特定的ID,那么任务就更容易了

foreach($cities_array as $key => $value)
{
if($key==id)
    echo $value['city'].' '.$value['region'].' '.$value['country'].' '.$value['latitude'].' '.$value['longitude']."\r\n";
}

答案 1 :(得分:0)

如果您知道该ID,则可以直接访问您的城市名称:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.print.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>unknown-jars-temp-repo</id>
        <name>"mvn deploy:deploy-file -Durl=file:///var/www/projetos/041print-proc/lib/ -Dfile=hibernate-core-4.3.10.Final.jar -DgroupId=org.hibernate -DartifactId=hibernate-core -Dpackaging=jar -Dversion=4.3.10.Final"</name>
        <url>file:${project.basedir}/lib</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.10.Final</version>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4-1201-jdbc41</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.12</version>
    </dependency>
    <dependency>
        <groupId>org.apache.xmlbeans</groupId>
        <artifactId>xmlbeans</artifactId>
        <version>2.6.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.12</version>
    </dependency>
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.3</version>
    </dependency>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.9</version>
    </dependency>
</dependencies>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>
<name>041 Print Processadores</name>
<description>Processadores de Texto para Dados Variáveis</description>

答案 2 :(得分:0)

您可以使用

加入城市名称
$cities_array[your_id]['city'];