|
|
|
> I`m writting a session-management system like i`ve
> seen in asp. There I can give the hash-array "session" an
> other array as a value.
>
> for( ...
> for( ...
> $arr[$i][$j] = "${i}_x_${j}";
> }
> }
>
> $session("arr") = $arr;
>
> Now I`ve written a routine based on "each" who gives me the
> values of the$session-array back as string.
>
> But not if the value is an other array - here I receive only
> the "Array" text.
>
> My question now is: how can i resolve ths kind of array in an
> array.
> A snip of code looks like:
>
> while( list( $key, $val ) = each( $session ) ) {
> if( $val = `Array` ) {
> $ts .= Split_Array( $???? );
> }
> else if( $val <> `` ) {
> $ts .= "session[".$key."]=$val&";
> }
> }
Well, basically you have already solved it. You know how to split up an
array, so just do the same thing again. And as soon as you utter that
nasty little "do the same thing again" the word, "recursion" should pop
into your head. You are in luck though, I have a million things to do, so
I procrastinated and wrote you a little recursive function that will
handle 1 to 4 dimensional arrays. For example, you can do this:
<?
for($i=0;$i<3;$i++) {
for($j=0; $j<3;$j++) {
$arr[$i][$j] = "${i}_x_${j}";
}
}
$session["two"] = $arr; // 3-dimensional
$session["zero"] = "123"; // 1-dimensional
$session["one"] = array( "first_index" => "Hello",
"second_index" => "World"); // 2-dimensional
$ts = Split_Array($session);
echo $ts;
?>
Now, the above mess should spit out a massive string that looks like this:
session[arr][0][0]=0_x_0&session[arr][0][1]=0_x_1&session[arr][0][2]
=0_x_2&session[arr][1][0]=1_x_0&
session[arr][1][1]=1_x_1&session[arr][1][2]=1_x_2&session[arr][2][0]
=2_x_0&session[arr][2][1]=2_x_1&
session[arr][2][2]=2_x_2&session[test]=123&session[one][first_index]
=Hello&session[one][second_index
]=World&
That`s quite a mouthful, but as far as I can tell from your question, this
is what you are trying to generate. So, we need an extremely magical
recursive Split_Array() function. And here it is:
<?
function Split_Array($array,$first="",$second="",$third="") {
while(list($key,$val) = each($array)) {
if(is_array($val))
$ret .= Split_Array($array[$key] ,
(strlen($first) ? $first:$key),
(strlen($second) ? $second:(strlen($first)?$key:"")),
(strlen($third) ? $third:(strlen($second)?$key:"")));
else {
$ret .= "session";
if(strlen($first)) $ret .= "[$first]";
if(strlen($second)) $ret .= "[$second]";
if(strlen($third)) $ret .= "[$third]";
$ret .= "[$key]=$val&";
}
}
return $ret;
}
?>
I bet a few people will have to chew on this one for a while to try to
figure out what the heck I am doing. Basically it is a recursive function
with optional arguments. Each level of recursion gets another argument so
we can keep track of the indices since by the time we nest deep enough to
get to the final value we have to have all the indices that took us there.
This one is probably a keeper, so the example page maintainers out there
should probably do something with this one. If nothing else, it shows
that PHP isn`t only for the weekend web warrior. You can write some
pretty tricky stuff. Well, relatively speaking anyway. It can get a lot
trickier than this, as well.
One thing to note here is that n-dimensional arrays like this are not
going to get parsed correctly if you set a cookie to the value of the
above. So, I suspect your effort may be in vain, although you don`t
mention what you plan on using this for.
|
|
| PHP Script to find url links in a page Categories : PHP, URLs, Regexps, Arrays | | | Tag content retrieval from websites with preg_match Categories : PHP, Regexps, Arrays, HTML and PHP | | | Array values from javascript to php Categories : PHP, Java Script, Arrays | | | clearing variables in php3 Categories : Variables, Arrays, PHP | | | Weighted Random - Random Scripts usually chose one out of each item, and each item have an equal chance to be chosen. But what if you want an item to be chosed more frequently than other? Categories : PHP, Math., Arrays | | | Display list of files within current and subdirectories (recursively) showing
each file as an anchored link and each directory as a category header. Categories : Filesystem, Directories, Arrays, PHP | | | Array Insertion Categories : PHP, PHP Classes, Arrays | | | Check for functional file links (broken Files)
Categories : PHP, Data Validation, FTP, Regexps, Arrays | | | Single-file PHP news system with automatic folder structure creation Categories : PHP, Filesystem, Arrays | | | This gets the http response headers for a given url and returns them in an assoc array. i.e. to test if a url exists: $array = get_http_headers($url); if($array[result]=200) { } Categories : HTTP, Arrays, PHP | | | Beginners Array Functions Categories : PHP, Beginner Guides, Arrays | | | How to pass an array from one PHP Script to another via an HTML form Categories : PHP, HTML and PHP, Arrays | | | How to load a query result into a PHP Array Categories : PHP, Databases, Arrays, MySQL | | | dynamic table columns Categories : PHP, HTML and PHP, Arrays, Databases, MySQL | | | A recursive function to traverse a multi-dimensional array where the
dimensions are not known Categories : Arrays, PHP, Algorithms | |
|
|
|