2013-02-19 5 views
-3
Array ( 

    [x0] => sometext1  
    [x1] => sometext2  
    [x2] => sometext3  
    [x3] => sometext4  
    [x4] => sometext5  
    [x5] => sometext6 
    [x?] => sometext? 

    [y0] => someothertext1  
    [y1] => someothertext2 
    [y2] => someothertext3  
    [y3] => someothertext4  
    [y4] => someothertext5  
    [y5] => someothertext6 
    [y?] => someothertext? 

    ) 

J'essaye de prendre ce tableau et de créer une table. Fondamentalement, je ne sais pas combien de x ou de y seront ... Je n'ai pas trouvé de solution pour l'instant. Toute aide serait grandement appréciée.Tableaux PHP à table sans succès

Le résultat que je suis en train d'obtenir

Tableau

x  |  y 
sometext1 | someothertext1 
sometext2 | someothertext2 
...  | ... 
+2

Quelle est la table que vous voulez? –

+2

que sont ces lignes et colomètres x et y? –

+0

x et y seront des colonnes. Je ne pouvais pas comprendre comment obtenir ces valeurs dans les lignes de la table. – user1605661

Répondre

0

Copier ce tableau dans un autre var pour cet exemple, vous avez deux tableaux avec les noms $ arr et arr2 $ qui est juste une copie de chaque.

$arr = $arr2 = Array ( ....); 

foreach($arr as $key=>$data) 
{ 
    $num = substr($key,1); 
    echo '<tr><td>'.$data.'</td>'; 
    foreach($arr2 as $key2=>$data2) 
    { 
     $num2 = substr($key2,1); 
     if($num == $num2) 
     { 
      echo '<td>'.$data.'</td>'; 
      break; 
     } 
    } 
    echo '</tr>'; 
} 

Bien sûr, ne tient pas compte des choses comme la fixation d'un td manquant s'il n'y a pas « y » pour correspondre à un « x » supplémentaire.

1

Si possible, effectuez une petite correction sur la baie, sur la façon dont elle est déclarée. Faites-regarder cette façon:

Array ( 
    [x] => Array (
     [0] => sometext1  
     [1] => sometext2  
     [2] => sometext3  
     [3] => sometext4  
     [4] => sometext5  
     [5] => sometext6 
     [?] => sometext? 
    ) 
    [y] => Array (
     [0] => sometext1  
     [1] => sometext2  
     [2] => sometext3  
     [3] => sometext4  
     [4] => sometext5  
     [5] => sometext6 
     [?] => sometext? 
    ) 
) 

Et de cette façon, en utilisant PHP, vous pouvez faire la table de cette façon:

<table> 
<?php 
    echo '<tr>'; 
    foreach ($array as $heading => $contents) 
     echo "<th>$heading</th>"; 
    echo '</tr>'; 
    foreach ($array as $heading => $contents) 
     foreach ($heading as $value) 
      echo "<td>$value</td>"; 
?> 
</table> 
+0

Merci @Praveen, mais en fait je ne sais pas comment je peux le convertir à votre édition. Pouvez-vous aider à comprendre? – user1605661

+0

@ user1605661: vérifiez ma réponse pour ce format –

+0

@ user1605661 Bon, alors la réponse de Prasanth vous conviendra. Le mien sera meilleur pour la maintenabilité. –

0

Comme Praveen Kumar a expliqué la table ressemble à ceci:

# X    y 
0 sometext1  sometext1 
1 sometext2  sometext2 
1

Essayez ceci:

<?php 
$array = Array('x0' => 'sometext1',  
       'x1' => 'sometext2',  
       'x2' => 'sometext3',  
       'x3' => 'sometext4',  
       'x4' => 'sometext5',  
       'x5' => 'sometext', 
       'y0' => 'someothertext1',  
       'y1' => 'someothertext2', 
       'y2' => 'someothertext3',  
       'y3' => 'someothertext4',  
       'y4' => 'someothertext5',  
       'y5' => 'someothertext6', 
       ); 

$res = array();    
foreach($array as $key=>$val){ 
    preg_match('/(?P<var>\w{1})(?P<ky>\d+)/',$key, $match); 
    $res[$match['ky']][$match['var']] = $val; 
} 


echo "<table>"; 
echo "<tr><td>X</td><td>Y</td></tr>"; 
foreach($res as $keys=>$vals){ 
    echo "<tr><td>".$vals['x']."</td><td>".$vals['y']."</td></tr>"; 
} 
echo "<table>"; 

?> 

Sortie:

X   Y 
sometext1 someothertext1 
sometext2 someothertext2 
sometext3 someothertext3 
sometext4 someothertext4 
sometext5 someothertext5 
sometext someothertext6 
+0

essayez avec ce tableau '$ array = Array ('x0' => 'sometext1', 'x1' => 'sometext2', 'x2' => 'sometext3', 'x3' => 'sometext4', 'x4' => 'sometext5', 'x11' => 'someText', 'y0' => 'someothertext1', 'y1' => 'someothertext2', 'y2' => 'someothertext3', 'Y3' => 'someothertext4', 'Y4' => 'someothertext5', 'Y11' => 'someothertext6', ) ' –

+0

Edité la réponse, je suppose que cela est celui que vous avez besoin –

+0

@SherinJose : cela fonctionne bien avec votre tableau –

Questions connexes