note 13074 modified in function.serialize by aidan

From: Date: Thu, 24 Jun 2004 10:20:03 +0000
Subject: note 13074 modified in function.serialize by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-71579@lists.php.net to get a copy of this message
If you're having trouble with serialized strings, try this function. Pass it the name of an array whose value you want to include in hidden form inputs, a url, or session variable. Works with strings or multidimensional arrays. <?php ////////////////////////////////////////////// // propogate v0.8 by sbarnum // method can be 'form' or 'url' or 'session' ////////////////////////////////////////////// function propogate ($arrayName, $method='form', $arrayData=''){ if($arrayData){ // custom values passed to array, use these instead of globals ${$arrayName} = $arrayData; }else{ // use global values global ${$arrayName}; } if ($method == "session"){ session_register(${$arrayName}); return TRUE; } @reset(${$arrayName}); while (list($key,$value)=@each(${$arrayName})){ if (is_array($value)){ $subArrayName = $arrayName . "[" . $key . "]"; $html .= propogate ($subArrayName, $method, $value); }else{ switch ($method){ case "form": $html .= "<INPUT type='hidden' name='".$arrayName."[".$key."]' value='".htmlspecialchars($value)."'>\n"; break; case "url": $html .= "&".$arrayName."[".$key."]=" . urlencode($value); break; } } } return $html; } ?> --was-- If you're having trouble with serialized strings, try this function. Pass it the name of an array whose value you want to include in hidden form inputs, a url, or session variable. Works with strings or multidimensional arrays. function propogate ($arrayName, $method='form', $arrayData=''){ ////////////////////////////////////////////// // propogate v0.8 by sbarnum // method can be 'form' or 'url' or 'session' ////////////////////////////////////////////// if($arrayData){ // custom values passed to array, use these instead of globals ${$arrayName} = $arrayData; }else{ // use global values global ${$arrayName}; } if ($method == "session"){ session_register(${$arrayName}); return TRUE; } @reset(${$arrayName}); while (list($key,$value)=@each(${$arrayName})){ if (is_array($value)){ $subArrayName = $arrayName . "[" . $key . "]"; $html .= propogate ($subArrayName, $method, $value); }else{ switch ($method){ case "form": $html .= "<INPUT type='hidden' name='".$arrayName."[".$key."]' value='".htmlspecialchars($value)."'>\n"; break; case "url": $html .= "&".$arrayName."[".$key."]=" . urlencode($value); break; } } } return $html; } http://php.net/manual/en/function.serialize.php

« previous php.notes (#71579) next »