String_to_array et array_to_string ?

pioug43 Messages postés 32 Date d'inscription lundi 23 décembre 2002 Statut Membre Dernière intervention 3 novembre 2006 - 10 août 2005 à 00:18
arnal69130 Messages postés 445 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 22 mars 2007 - 10 août 2005 à 15:51
Bonjour,



Je recherhce une fonction qui pourrait me traduire
un tableau vers une string et inversement de plus la fonction etre
recurcive puisqu'un tableau peut aussi contenir un tableau

J'ai trouve ce bout de code mais je n'arrive pas à voir comment je
pourrais integre qu'un tableau peut aussi contenir un tableau et ainsi
de suite



// Converts an array to a string that is safe to pass via a URL

function array_to_string($array) {

    $retval = '';

    foreach ($array as $index => $value) {

        $retval .= urlencode(base64_encode($index)) . '|' . urlencode(base64_encode($value)) . '||';

    }

    return urlencode(substr($retval, 0, -2));

}

    

// Converts a string created by array_to_string() back into an array.

function string_to_array($string) {

    $retval = array();

    $string = urldecode($string);

    $tmp_array = explode('||', $string);

    foreach ($tmp_array as $tmp_val) {

        list($index, $value) = explode('|', $tmp_val);

        $retval[base64_decode(urldecode($index))] = base64_decode(urldecode($value));

    }   

    return $retval;

}




Merci

6 réponses

arnal69130 Messages postés 445 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 22 mars 2007 2
10 août 2005 à 10:18
En utilisant les fonctions implode et explode (http://fr2.php.net/manual/fr/function.implode.php), on peut faire qqch du genre :

function tableauVersChaine($sep,$tableau){
foreach($tableau as $cle=>$val){
if (is_array($val))
$tableau[$cle]=tableauVersChaine($sep,$val);
}
return implode($sep,$tableau);
}

Je cherche pour l'autre sens...
Arn;o)
0
arnal69130 Messages postés 445 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 22 mars 2007 2
10 août 2005 à 11:06
<?php
function tableauVersChaine($sep,$tableau
){
foreach($tableau as $cle=>$val
){
if (is_array($val
))
$tableau[$cle]='['.tableauVersChaine($sep,$val).']'
;
}
return implode($sep,$tableau
);
}
function chaineVersTableau($sep,$chaine
){
$tChaine=str_split($chaine
);
$tRes
=array();
$mot=''
;
$sousTableau=0
;
foreach($tChaine as $c
){
if ($c=='['
)
$sousTableau
++;
if ($c==']'
)
$sousTableau
--;
if (($c != $sep) || ($sousTableau>0
))
$mot.=$c
;
if ($c == $sep
) {
if ($sousTableau>0
)
continue;
$tRes[]=$mot
;
$mot=''
;
}
}
$tRes[]=$mot
;
foreach($tRes as $n=>$mot
) {
if ($mot{0}=='['
)
$tRes[$n]=chaineVersTableau($sep,substr($mot,1,strlen($mot)-2
));
}
return $tRes
;
}
$tab1= array('nom', array('tty','tty2',array('encore un autre tableau','ok alors ?'),'tty3'),'email', 'telephone', array('val1','val2'
));
echo 'Tableau1 : '
;
print_r($tab1
);
$chaine1= tableauVersChaine(",", $tab1
);
echo 'chaine1 : '.$chaine1.'
'
;
$tab2=chaineVersTableau(",",$chaine1
);
echo '-> tab2 : '
;
print_r($tab2);

Et voila . Pour la conversion chaine->tableau ce fut un peu plus compliqué, et il peut-être possible d'optimiser la fonction.

Arn;o)
0
pioug43 Messages postés 32 Date d'inscription lundi 23 décembre 2002 Statut Membre Dernière intervention 3 novembre 2006
10 août 2005 à 12:43
Ca marche pas mal par contre en fait j'essay de traduire une chaine provenant de $_POST



chaine en entrée:



Array
(
[PHPSESSID] => d3e46ae99d46d1873b5de3aa76a38aed
[config] => Array
(
[comments_enabled] => 0
[beautify_comments_text] => 1
[show_posts_max] => 15
[recent_posts_max] => 10
[save_drafts_via_xmlhttprequest_enabled] => 1
[default_locale] => fr_FR
[default_time_offset] => 0
[html_allowed_tags_in_comments] =>




Merci je vais essayer d'adapter ta fonction
0
arnal69130 Messages postés 445 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 22 mars 2007 2
10 août 2005 à 14:21
En fait, il ya bcp plus simple :
Regarde les fonctions serialize et unserialize :
http://fr2.php.net/manual/fr/function.serialize.php

Arn;o)
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
pioug43 Messages postés 32 Date d'inscription lundi 23 décembre 2002 Statut Membre Dernière intervention 3 novembre 2006
10 août 2005 à 15:32
C pas mal mais le probleme c que je suis sur deux serveurs differents
et la fonction serialize et unserialize doivent etre utilise sur le
meme serveur



Merci
0
arnal69130 Messages postés 445 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 22 mars 2007 2
10 août 2005 à 15:51
Ah bon ? je n'ai pas vu ça... c'est marqué où ?
Tu partages tes données entre 2 sites qui ne sont pas sur le même serveurs ?
0
Rejoignez-nous