php associative array:没有使用变量名的键构建数组

时间:2017-09-08 18:18:10

标签: php simplexml

此代码按预期工作

$fruits = array("d" => "Zitrone", "b" => "Banane", "c" => "Apfel");
$fruits["a"] = "Orange";
//print_r($fruits);
ksort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}

好的,现在我想以编程方式执行相同的操作:

$collection = array();
foreach ($xml->abschnitte[0]->abschnitt as $sec) {
    //echo $sec['strecke']; // this works, the strings are printed out
    $collection[$sec['strecke']] = $sec['id'];
}
//print_r($collection);  // nothing to see here
//ksort($collection);
foreach ($collection as $key => $val) {
    echo $key . " = " . $val . "\n";
}

似乎没有可以构建的集合。但必须有一种方法来从变量构建密钥。我想念什么?提前谢谢,mischl

0 个答案:

没有答案