如何从外部数组PHP读取数据?

时间:2013-12-13 01:02:11

标签: php arrays get external

我有两个文件: config.php中:

$config["stations"]["aacplus"] = array( 
    "host" => "11.111.111.111",
    "port" => "9999",
    "password" => "aacplusv3",
            "logo" => "http://cassianohost.com.br/amostra/sualogo.jpg"
);
$config["stations"]["aacplus"] = array( 
    "host" => "22.222.222.222.222",
    "port" => "8888",
    "password" => "aacplusv3",
            "logo" => "http://cassianohost.com.br/amostra/sualogo.jpg"
);`

我有一个索引:

    <?      
session_start();
$porta = $_SESSION["config"]["array"]["stations"]["aacplus"]["$port"];
include("config.php");?>
<html>
Porta: <?echo $port ?>

如何捕获索引php config中的端口?

3 个答案:

答案 0 :(得分:0)

这是一个多维数组,因此您可以访问第三级,如:

$Port = $config["stations"]["aacplus"]["port"];

但是,请注意警告,您正在用第二个数组覆盖$config,因此只能访问一个端口。为避免这种情况,请执行以下操作:

//                      note: vv
$config["stations"]["aacplus"][] = array( 
"host" => "11.111.111.111",
"port" => "9999",
"password" => "aacplusv3",
        "logo" => "http://cassianohost.com.br/amostra/sualogo.jpg"
);
$config["stations"]["aacplus"][] = array( 
"host" => "22.222.222.222.222",
"port" => "8888",
"password" => "aacplusv3",
        "logo" => "http://cassianohost.com.br/amostra/sualogo.jpg"
);

然后你可以拥有一个简单的数组,其端口如下:

$Ports = array();
foreach ($config["stations"]["aacplus"] as $Station)
  {
  $Ports[] = $Station["port"];
  }

然后在索引中显示所有端口:

foreach ($Ports as $Port)
  {
  echo $Port . "<br>";
  }

答案 1 :(得分:0)

这是错误的["$port"];不包含$,因为您试图获取具有名为port

键的数组的值
$configport = $config["stations"]["aacplus"];
echo $configport["port"];

答案 2 :(得分:0)

你的两个阵列互相覆盖。如果您希望它们是内联的,则需要执行

$config["stations"]["aacplus"] = array();
$config["stations"]["aacplus"][] = array( 
    "host" => "11.111.111.111",
    "port" => "9999",
    "password" => "aacplusv3",
            "logo" => "http://cassianohost.com.br/amostra/sualogo.jpg"
);
$config["stations"]["aacplus"][] = array( 
    "host" => "22.222.222.222.222",
    "port" => "8888",
    "password" => "aacplusv3",
            "logo" => "http://cassianohost.com.br/amostra/sualogo.jpg"
);

然后你可以做

echo $config["stations"]["aacplus"][0]["port"];
echo $config["stations"]["aacplus"][1]["port"];