Précédent   Forum des professionnels en informatique > PHP > Scripts
Scripts Forum d'entraide sur les scripts PHP téléchargés. Les meilleurs scripts PHP, la FAQ scripts PHP, toutes les FAQ PHP
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 24/02/2011, 15h48   #1
Membre habitué
 
Avatar de omar24
 
Homme Omar
Inscription : septembre 2010
Messages : 146
Détails du profil
Informations personnelles :
Nom : Homme Omar
Localisation : Maroc

Informations professionnelles :
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : septembre 2010
Messages : 146
Points : 125
Points : 125
Par défaut script php pour convertir un array en xml

je suis en train de modifier le script array2xml
voila ce que mon nouveau code:
Code php :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
function is_assoc($var)
{
        return is_array($var) && array_diff_key($var,array_keys(array_keys($var)));
}
 
function array2xml($buffer) {
    $xml  = htmlspecialchars('<?xml version="1.0" encoding="utf-8"?>').'<br/>';
    $xml .= htmlspecialchars('<service version="1.0"\>').'<br/>';	
    foreach($buffer as $val) {
        $xml .= htmlspecialchars('    <document>').'<br/>';
        foreach ($val as $key => $value) {
            $xml .= htmlspecialchars('        <'.$key.'>');
			if(is_assoc($value)) {
			//echo '<br/>';
				foreach($value as $va) {
					foreach ($va as $k => $v) {
						$xml .= htmlspecialchars ('<'.$k.'>'.utf8_encode($v).'</'.$k.'>').'<br/>';
					}
				}
				$xml.=htmlspecialchars('</'.$key.'>').'<br/>';
			} else $xml.=htmlspecialchars(utf8_encode($value).'</'.$key.'>').'<br/>';
        }
		$xml .= htmlspecialchars('    </document>').'<br/>';
    }
    $xml .= htmlspecialchars('</service>').'<br/>';    
    return $xml;
}
 
// exemple d'utilisation 
$ar = array("var1" => "val1","var2" => "val2");
$arr = array(
                array("id" => "1", 
                      "pseudo" => "mq", 
                      "name" => "marc",
					  "tab" => $ar),
                array("id" => "2",
                      "pseudo" => "poppy",
                      "name" => "axel")
            );
 
$xml = array2xml($arr);
echo $xml;
?>
quand j’exécute j'ai ce qui suit:

Citation:
Warning: Invalid argument supplied for foreach() in C:\Program Files\Apache Group\Apache2\htdocs\wdCalendar\wdCalendar\php\tist.php on line 17

Warning: Invalid argument supplied for foreach() in C:\Program Files\Apache Group\Apache2\htdocs\wdCalendar\wdCalendar\php\tist.php on line 17
<?xml version="1.0" encoding="utf-8"?>
<service version="1.0"\>
<document>
<id>1</id>
<pseudo>mq</pseudo>
<name>marc</name>
<tab></tab>
</document>
<document>
<id>2</id>
<pseudo>poppy</pseudo>
<name>axel</name>
</document>
</service>
ou est l'erreur?
omar24 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 02/03/2011, 09h06   #2
Membre régulier
 
jean-michel gens
retraité
Inscription : mars 2008
Messages : 116
Détails du profil
Informations personnelles :
Nom : jean-michel gens
Âge : 62
Localisation : France

Informations professionnelles :
Activité : retraité
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : mars 2008
Messages : 116
Points : 93
Points : 93
Envoyer un message via ICQ à jean-michel-78 Envoyer un message via Yahoo à jean-michel-78 Envoyer un message via Skype™ à jean-michel-78
ton script n'est pas récursif donc il explore qu'un niveau dans les tableaux (array)
peut etre que $result=var_export($my_array,true) t'aiderait
et tu bricoles sur $result
jean-michel-78 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 02/03/2011, 09h53   #3
Membre régulier
 
jean-michel gens
retraité
Inscription : mars 2008
Messages : 116
Détails du profil
Informations personnelles :
Nom : jean-michel gens
Âge : 62
Localisation : France

Informations professionnelles :
Activité : retraité
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : mars 2008
Messages : 116
Points : 93
Points : 93
Envoyer un message via ICQ à jean-michel-78 Envoyer un message via Yahoo à jean-michel-78 Envoyer un message via Skype™ à jean-michel-78
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
<?php
echo "<PRE>";
$a=array("nom"=>"toto rigolo","enfants"=>array("vincent"=>array("jeremie"),"irene"),"age"=>"60");
$c=var_export($a,TRUE);
$t=explode("\n",$c);
$indent=0;
foreach ($t as $v) {
        $v=trim($v);
        if ($v=="array (") { $indent++;continue;}
        for ( $i=0; $i< $indent ;$i++) echo "\t";
        echo "$v\n";
        if ( strstr($v,')')) {$indent--;continue;}
}
jean-michel-78 est déconnecté   Envoyer un message privé Réponse avec citation 01
Vieux 02/03/2011, 10h43   #4
Modérateur
 
Inscription : septembre 2010
Messages : 7 101
Détails du profil
Informations forums :
Inscription : septembre 2010
Messages : 7 101
Points : 8 465
Points : 8 465
pourquoi extract ????
__________________
http://blog.stealth35.com/
stealth35 est déconnecté   Envoyer un message privé Réponse avec citation 01
Vieux 02/03/2011, 14h22   #5
Membre régulier
 
jean-michel gens
retraité
Inscription : mars 2008
Messages : 116
Détails du profil
Informations personnelles :
Nom : jean-michel gens
Âge : 62
Localisation : France

Informations professionnelles :
Activité : retraité
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : mars 2008
Messages : 116
Points : 93
Points : 93
Envoyer un message via ICQ à jean-michel-78 Envoyer un message via Yahoo à jean-michel-78 Envoyer un message via Skype™ à jean-michel-78
parce que j ai pris un bout de code que j ai modifié les deux premieres lignes ne sont pas utiles désolé
j ai modifié
jean-michel-78 est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 16h41.


 
 
 
 
Partenaires

Hébergement Web