2011-12-19 65 views
1

我試圖從每個子數組中回顯存儲在key ='Tipo'中的值。嵌套的foreach從子數組中回顯密鑰?

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
<?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

+0

感謝,is_numeric修復了這些問題。改變它與給定的數組一起工作 – lisovaccaro