Warum funktioniert die list funktion nicht ?

Dieses script :

<?
$array = [
    [1, 2],
    [3, 4],
];

foreach ($array as list($a)) {
    // Note that there is no $b here.
    echo "$a
";
}
?>

sollte laut PHP Doku http://www.php.net/manual/de/control-structures.foreach.php

laufen. Erzeugt aber leider die folgende Fehlermeldung :

Parse error: syntax error, unexpected ‚list‘ (T_LIST) in C:\IP-Symcon\scripts\58723.ips.php on line 7
Abort Processing during Fatal-Error: syntax error, unexpected ‚list‘ (T_LIST)
Error in Script C:\IP-Symcon\scripts\58723.ips.php on Line 7?>

wie dort auch steht: PHP>=5.5. IPS hat aber noch nicht PHP5.5.

Die list Funktion funktioniert schon, aber nicht für ‚nested arrays‘ (erst ab PHP 5.5)

Du kannst folgendes mit list versuchen:
(ich weiß zwar nicht was du machen möchtest)


<?
$array = [
    [1, 2],
    [3, 4],
];
reset($array);
while (list($key, $subarrays) = each($array)) {
    while (list($subkey, $value) = each($subarrays)) {
        echo "Key: $subkey; Value: $value<br />
";
     }
}
?>

Gruß
Günter

Wenn ich die foreach funktion auf der zweiten Ebene des Arrays laufen lassen will geht das nur in Kombination mit der list funktion :

<?php
$array = [
    [1, 2],
    [3, 4],
];

foreach ($array as list($a, $b)) {
    // $a contains the first element of the nested array,
    // and $b contains the second element.
    echo "A: $a; B: $b
";
}
?>

Output :
A: 1; B: 2
A: 3; B: 4

Du hast natürlich recht das man das Problem auch anders lösen kann. Ich fand halt das die list funktion damit sehr elegant umgeht und jetzt nachdem du die php 5.5 nennst habe ich das im php manual auch gesehen… die berühmten Tomaten auf den Augen scheinen wieder zu wachsen :frowning: