Page 1 sur 1

Affichage de tableau

Posté : 08 déc. 2013, 17:08
par ouryhamdalaye
Bonjour, :o
je suis débutant en php :oops: , et j'essaye d'afficher le contenu d'un tableau associatif, cependant je ne comprends pas pourquoi en résultat il m'affiche : "array,array,array..." au lieu de m'afficher le contenu du tableau. Pouvez vous m'aider ?

Voici mon code :
<?php
error_reporting(E_ALL);
ini_set('display_errors','true');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>titre</title>
<link rel="stylesheet" type="text/css" href="fichier.css" />
</head>

<body>
<?php

function display($tableau)
{
	foreach($tableau as $v)
	{
		echo $v." ";
	}
	print_r($tableau);
}

$t = array(
array("D","U","T"),
array("I","N","F","O"),
array("G","E","E","K"),
array("S","C","H","O","O","L")
);

display($t);

?>
</body>
</html>
J'ai aussi essayé un
 foreach($tableau as $k=>$v){ echo $v." ";} 
mais sans résultat

Merci beaucoup :D

Re: Affichage de tableau

Posté : 08 déc. 2013, 17:35
par nico63800
bonjour, c'est normal
<?php
$t = array(
array("D","U","T"),
array("I","N","F","O"),
array("G","E","E","K"),
array("S","C","H","O","O","L")
);
?>
ton tableau $t est un tableau de tableau, chaque élément de $t est un tableau d’où l'affichage : "array,array,array..."

modifies ta fonction comme çà:
function display($tableau)
{
  foreach($tableau as $v)
  {
    if(is_array($v))
    {
      foreach($v as $val)    {  echo $val." ";  }
    }
    else
    {
      echo $v." ";
    }
  }
}

Re: Affichage de tableau

Posté : 08 déc. 2013, 17:57
par nico63800
tu peux aussi utiliser une fonction récursive:
function printAll($a)
{
 if (!is_array($a))
  {
    echo $a, ' ';
    return;
  }

   foreach($a as $v)
   {
     printAll($v);
   }
 }

Re: Affichage de tableau

Posté : 08 déc. 2013, 18:49
par ouryhamdalaye
ReSalut,

Merci beaucoup ça fonctionne :D

Merci,