嵌套的foreach从子数组中回显一个键?

时间:2011-12-19 04:35:13

标签: php

我正在尝试从每个子数组中回显存储在key ='Tipo'中的值。

foreach ($palabras as $key => $palabra) {
    foreach ($palabra as $key2 => $meaning) {
        echo $palabra[$key2]['Tipo'];
        }
}

我确定我的错误非常简单,但我不确定它是什么。问题是我得到随机字符而不仅仅是值。

您知道这是原始数组:

Array
(
    [0] => Array
    (
        [Palabra] => juan
    )

[1] => Array
    (
        [Palabra] => es
        [0] => Array
            (
                [Raiz] => ser
                [Tipo] => verbo1
            )

        [1] => Array
            (
                [Raiz] => ser
                [Tipo] => verbo2
            )

    )

[2] => Array
    (
        [Palabra] => un
        [0] => Array
            (
                [Tipo] => articulo-indeterminado
            )

    )

[3] => Array
    (
        [Palabra] => adjetivo
        [0] => Array
            (
                [Tipo] => Clase
                [Clase] => Adjetivo
            )
     )
)

这就是我的代码所带来的:

jeverbo1verbo2uarticulo-indeterminadoaClase

1 个答案:

答案 0 :(得分:1)

<?php

$palabras[0]['palabra'] ='juan';
$palabras[1]['palabra'] ='es';
$palabras[1][0]['tipo']= 'verbo1';
$palabras[1][1]['tipo']= 'verbo2';
$palabras[2]['palabra'] ='blah';
$palabras[2][0]['tipo']= 'verb03';
$palabras[3]['palabra'] ='wha';
$palabras[3][0]['tipo']= 'adjectivo';
$palabras[3][1]['tipo']= 'verbo4';

print_r($palabras);

foreach ($palabras as $palabra) {
        foreach ($palabra as $key=>$parto) {
                if (is_numeric($key)) {
                        echo $parto['tipo'] . "<br>";
                }
        }
}

http://www.ideone.com/cXVAe