Page 2 of 2
Posted: Mon Jan 23, 2006 11:13 pm
by josh
AKA Panama Jack wrote:Everyone IGNORED the fact it should NOT use the unserialize function from PHP in any fashion.

No, we simply provided a method of achieving the desired effect due to the lack of specification on your part:
1) Does the data need to be loaded into an array or will it be outputted on the spot
(either way the methods me and feyd posted solve the problem)
2) WHY must the data not be loaded into an array, perhaps there is an alternate solution to your problem
No one is going to walk you through re-creating an existing PHP function just so you can avoid a 'temp' array (even though re-creating it would still require a temp array).
You have refused to address the issue of what exactly you are doing with the data, are you going to load it into an array and modify / display it somewhere else, if so the only con to using the native PHP functions are the data storage format itself. List one thing the unserialize() function
cannot do that your function would be able to do, no "it loads the variables when I don't want it to" is NOT a valid answer.
To further add to these arguments your pseudocode posted early on in the thread:
Code: Select all
$unserialized_list = unserialize_function($serialized_data); // this is a function that converts the serialised string into an array of what each variable in the array actually is
Please, no really... please explain to me how this does not create an array or expand the variables that were serialized
Also please avoid further rude statements that imply ignorance on our [all the responses in this thread] part by stating that we are ignoring you when we are simply providing code based on your specifications
-------------------------------------------------------------------------------------------------------------------
feyd wrote:it is jshpro2, but it doesn't need the "temporary" variable to store the output from unserialize() foreach does that for you

but foreach operates on a copy of the array, how about this:
Code: Select all
$array = unserialize($somedata);
if (!$count=count($array)) {
echo 'failed';
} else {
for ($i=0;$i<$count;$i++) {
// foo
}
}
(next you're going to re-write this in byte-codes to further optimize aren't you)
Posted: Tue Jan 24, 2006 1:25 am
by AKA Panama Jack
jshpro2 wrote:AKA Panama Jack wrote:Everyone IGNORED the fact it should NOT use the unserialize function from PHP in any fashion.

No, we simply provided a method of achieving the desired effect due to the lack of specification on your part:
I did provide specifications and even provided an example and what the output would be. (Yes I am ticked.)
jshpro2 wrote:You have refused to address the issue of what exactly you are doing with the data
I did address the issue but you and others decided to totally ignore it. I will try to explain it in a simplistic manner anyone should understand.
The serialized string needs to be converted into text listing all variables and their contents without actually creating the stored variables that unserialize ALWAYS creates.
Another SIMPLE METHOD...
The serialized text string is an encoded string and it needs to be decoded into a TEXT file/string without creating the variables it represents.
I don't know how many more ways I can say the same thing.
jshpro2 wrote:To further add to these arguments your pseudocode posted early on in the thread:
Code: Select all
$unserialized_list = unserialize_function($serialized_data); // this is a function that converts the serialised string into an array of what each variable in the array actually is
Please, no really... please explain to me how this does not create an array or expand the variables that were serialized
And again...
The $unserialized_list could just be a STRING variable instead of a STRING ARRAY variable and have all of the data inside the string like this.
Code: Select all
$unserialized_list = "$test[\'junk\'] = "My test"\n$test[0] = 56\n$test[\'stuff\'][\'mine\'] = "Last one"\n";
Which could be saved as a text file that would contain...
Code: Select all
$test['junk'] = "My test"
$test[0] = 56
$test['stuff']['mine'] = "Last one"
jshpro2 wrote:Also please avoid further rude statements that imply ignorance on our [all the responses in this thread] part by stating that we are ignoring you when we are simply providing code based on your specifications
I am not implying anything but stating fact. I clearly stated what was needed and that people should NOT respond using the unserialize command. Instead people ignored that part of the message.
It doesn't matter... It looks like no one wants to REALLY help and I don't consider it help to offer up examples using the unserialize function when I expressly said I didn't want that used.
I guess I will give a go at converting the PHP Array to XML output portion of this package...
http://pxw4pa.sourceforge.net/index.php
That will convert an array into a serialized string and then parse the string into XML data. Basically what I am wanting is to convert a serialized string into straight text data similar to how the above package converts into XML text.
I was hoping someone had heard of a routine to do something like this but apparently everyone was too fixated upon the unserialize command and didn't notice what I was wanting. The responses from the people who ignored the "do not use the unserialize command" caused frustration and absolutely no help.
Maybe I will post the code if I get it converted so you will see what everyone was ignoring.
Posted: Tue Jan 24, 2006 2:20 am
by AKA Panama Jack
I should have just sat down and done this in the first place instead of asking if anyone knew of something that would do this.
Code: Select all
<?php
function unserialized_list($serialized, $arrayname = "test", $include_semicolon = 0)
{
// explode serialized string to a temporary array
$i=0;
for($n=0;$n<strlen($serialized);$n++){
$unserial_data[$i].=$serialized[$n];
if ($serialized[$n]=='{') {$i++;$ii=0;}
if ($serialized[$n]=='}') {$i++;$ii=0;}
if ($serialized[$n]==';' and preg_match('`^"$|^[0-9]$`',$serialized[$n-1]) and preg_match('`^a:[0-9]+:{|^s:[0-9]+:"|^}?i:[0-9]+|^}`Usi',substr($serialized,$n+1,8))){$ii++;}
else if ($serialized[$n]==';' and preg_match('`^"$|^[0-9]$`',$serialized[$n-1]) and preg_match('`^a:[0-9]+:{|^s:[0-9]+:"|^}?d:[0-9]+|^}`Usi',substr($serialized,$n+1,8))){$ii++;}
else if ($serialized[$n]==';' and preg_match('`^"$|^N$`',$serialized[$n-1]) and preg_match('`^a:[0-9]+:{|^s:[0-9]+:"|^}?i:[0-9]+|^}`Usi',substr($serialized,$n+1,7))){$i++;$ii=0;}
if ($ii==2) {$ii=0;$i++;}
}
// fix all lines replacing php syntax used for serialization
$unserial_data[0]="";
foreach($unserial_data as $k=>$v){
// fix entries
if (preg_match('`s:[0-9]+:"(.+)";s:[0-9]+:"(.*)";`Usi',$v,$tmp)) {$unserial_data[$k]="['".$tmp[1]."'] = \"".$tmp[2]."\";";}
if (preg_match('`s:[0-9]+:"(.+)";N;`Usi',$v,$tmp)) { $unserial_data[$k]="['".$tmp[1]."'] = \"".$tmp[2]."\";";}
if (preg_match('`i:([0-9]+);N;`Usi',$v,$tmp)) { $unserial_data[$k]="['".$tmp[1]."'] = \"".$tmp[2]."\";";}
if (preg_match('`i:([0-9]+);s:[0-9]+:"(.*)";`Usi',$v,$tmp)) { $unserial_data[$k]="['".$tmp[1]."'] = \"".$tmp[2]."\";";}
// process double precision number
if (preg_match('`i:([0-9]+);d:(.*);`Usi',$v,$tmp)) { $unserial_data[$k]="['".$tmp[1]."'] = \"".$tmp[2]."\";";}
// process integer number
if (preg_match('`i:([0-9]+);i:([0-9]+);`Usi',$v,$tmp)) { $unserial_data[$k]="['".$tmp[1]."'] = \"".$tmp[2]."\";";}
if (preg_match('`s:[0-9]+:"(.+)";i:([0-9]+);`Usi',$v,$tmp)) { $unserial_data[$k]="['".$tmp[1]."'] = \"".$tmp[2]."\";";}
// fix groups
if (preg_match('`s:[0-9]+:"[^"]+";a:[0-9]+:{`Usi',$v)) {$unserial_data[$k]=preg_replace('`s:[0-9]+:"([^"]+)";a:[0-9]+:{`Ui',"['\$1']",$v);}
if (preg_match('`i:[0-9]+;a:[0-9]+:{`Usi',$v)) {$unserial_data[$k]=preg_replace('`i:[0-9]+;a:[0-9]+:{`Ui',";",$v);}
$unserial_data[$k]=str_replace('}','',$unserial_data[$k]);
}
$semicolon = "";
if($include_semicolon)
{
$semicolon = ";";
}
$list = explode(";", implode('', $unserial_data));
for($i = 0; $i < count($list); $i++)
{
if($list[$i] != '')
{
$list[$i] = "\$" . $arrayname . $list[$i] . $semicolon . "<br>";
}
}
return $list;
}
$array['stuff'] = "hi there";
$array[5] = 67;
$array['thing']['orange'] = "color";
$array[4] = 67.01;
$array['bla']['erm']['fruit'] = "color";
$serialized=serialize($array);
echo $serialized . "<br><br>";
$list = unserialized_list($serialized);
echo implode('', $list);
print "<br>Finished<br><br>";
$list = unserialized_list($serialized, "myarray");
echo implode('', $list);
print "<br>Finished<br><br>";
$list = unserialized_list($serialized, "anotherarray", 1);
echo implode('', $list);
print "<br>Finished<br><br>";
?>
It outputs...
Code: Select all
a:5:{s:5:"stuff";s:8:"hi there";i:5;i:67;s:5:"thing";a:1:{s:6:"orange";s:5:"color";}i:4;d:67.0100000000000051159076974727213382720947265625;s:3:"bla";a:1:{s:3:"erm";a:1:{s:5:"fruit";s:5:"color";}}}
$test['stuff'] = "hi there"
$test['5'] = "67"
$test['thing']['orange'] = "color"
$test['4'] = "67.0100000000000051159076974727213382720947265625"
$test['bla']['erm']['fruit'] = "color"
Finished
$myarray['stuff'] = "hi there"
$myarray['5'] = "67"
$myarray['thing']['orange'] = "color"
$myarray['4'] = "67.0100000000000051159076974727213382720947265625"
$myarray['bla']['erm']['fruit'] = "color"
Finished
$anotherarray['stuff'] = "hi there";
$anotherarray['5'] = "67";
$anotherarray['thing']['orange'] = "color";
$anotherarray['4'] = "67.0100000000000051159076974727213382720947265625";
$anotherarray['bla']['erm']['fruit'] = "color";
Finished
See, absolutely no use of the unserialize and it doesn't create any of the variables.
Next time I will just do it instead of asking if someone knew of something that was already written to do it. It's just a quickie edit and I could still add something to remove the quotes from the numeric only values.
I edited and reposted the code because the initial post wouldn't process floating point (d) elements in the serialized text.
Posted: Tue Jan 24, 2006 3:52 am
by Jenk
but that is an array of the elements from a serialized array.. which is what you specifically stated you did not want...
Drop the attitude, else you might find no one wants to help next time.
Posted: Tue Jan 24, 2006 5:20 am
by AKA Panama Jack
Jenk wrote:but that is an array of the elements from a serialized array.. which is what you specifically stated you did not want...
Drop the attitude, else you might find no one wants to help next time.
No it is NOT. Sigh...
It is a TEXT output. Look at the code.
Each element of the array is a TEXT string. And I combined all elimints into one text string variable. In no place do you see ANY of the variables every being created in the code.
You might want to actually understand what I wrote and what the output is from that code before commenting.
It's like using the FILE command to read in a TEXT file and placing each line of text into an array element. It parses the serialized string into TEXT unlike the unserialize function of PHP which parses the serialized string into VARIABLES. This is what I was looking for and even with code that shows exactly what it does people still don't understand it. :p
Posted: Tue Jan 24, 2006 6:20 am
by Jenk
so what's the difference between your code and:
Code: Select all
$string = implode('', unserialize($string));
?
Or even:
Code: Select all
$serialstring = serialize($array);
foreach (unserialize($serialstring) as $key => $val) {
$final .= "{$key} => {$val}\n";
}
echo $final;
Or for a recursive solution..
Code: Select all
function array2str ($array) {
$output = '';
foreach ($array as $key => $val) {
if (is_array($val)) {
$output .= . "[{$key}]" . array2str($val);
} else {
$output .= "[{$key}] => {$val}\n";
}
return $output;
}
echo array2str(unserialize($serialstring));
or you can just ignore the above and go for plain old:
Code: Select all
print_r(unserialize($serialstring));
or var_dump..
So why bother going to all this hassle just to avoid the use of unserialize? Oh and I would read your code if it wasn't so painful to the eye. Whitespace (proper use of it) is your friend.
Posted: Tue Jan 24, 2006 8:16 am
by JayBird
If we are going to continue this discussion, please don't fire personal insults at each other.
I think this all could be solved if the exact reason why you didn't want to use unserialize() was given.
Without undestanding your exact intentions, your code seems overkill

... but as i said, it may not be if the precise reason for soign it this way was given.
Remember, keep it friendly

Posted: Tue Jan 24, 2006 9:29 am
by Chris Corbyn
This is pointless.... why are you so set against unserializing the data? If it's an issue with globals then it's not going to have an impact when run at a local scope rather than global

Are you merely curious or is it actually *needed* this way? It might be easier to come up with a solution of we knew the reaosning behind your reluctancy to unserialize the data
EDIT | Ooops... completely missed the fact there was a second page.
Posted: Tue Jan 24, 2006 11:42 am
by josh
Posted: Tue Jan 24, 2006 8:06 pm
by Jenk
Output from your function:
Code: Select all
>E:\PHP\php.exe -q test2.php
a:5:{s:5:"stuff";s:8:"hi there";i:5;i:67;s:5:"thing";a:1:{s:6:"orange";s:5:"color";}i:4;d:67.0100000000000051159076974727213382720947265625;s:3:"bla";a:1:{s:3:"erm";a:1:{s:5:"fruit";s:5:"color";}}}<br><br>
Notice: Undefined variable: unserial_data in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 0 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
Notice: Undefined offset: 1 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 2 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 3 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 4 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 5 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 6 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 7 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 8 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 9 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 10 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 11 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 12 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
$test['stuff'] = "hi there"<br>$test['5'] = "67"<br>$test['thing']['orange'] = "color"<br>$test['4'] = "67.0100000000000051159076974727213382720947265625"<br>$test['bla']['erm']['fruit'] = "color"<br><br>Finished<br><br>
Notice: Undefined variable: unserial_data in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 0 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
Notice: Undefined offset: 1 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 2 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 3 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 4 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 5 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 6 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 7 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 8 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 9 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 10 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 11 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 12 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
$myarray['stuff'] = "hi there"<br>$myarray['5'] = "67"<br>$myarray['thing']['orange'] = "color"<br>$myarray['4'] = "67.0100000000000051159076974727213382720947265625"<br>$myarray['bla']['erm']['fruit'] = "color"<br><br>Finished<br><br>
Notice: Undefined variable: unserial_data in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined variable: unserial_data in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 0 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
PHP Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
PHP Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
PHP Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
PHP Notice: Undefined offset: 1 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 2 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 3 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 4 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 5 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 6 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 7 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 8 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 9 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 10 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 11 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 12 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined variable: unserial_data in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 0 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
PHP Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
PHP Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
PHP Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
PHP Notice: Undefined offset: 1 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 2 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 3 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 4 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 5 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 6 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 7 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 8 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 9 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 10 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 11 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 12 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined variable: unserial_data in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 0 in E:\Program Files\Apache Group\A
Notice: Undefined offset: 0 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
Notice: Undefined offset: 1 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 2 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 3 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 4 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 5 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 6 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 7 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 8 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 9 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 10 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 11 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
Notice: Undefined offset: 12 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
$anotherarray['stuff'] = "hi there";<br>$anotherarray['5'] = "67";<br>$anotherarray['thing']['orange'] = "color";<br>$anotherarray['4'] = "67.0100000000000051159076974727213382720947265625";<br>$anotherarray['bla']['erm']['fruit'] = "color";<br><br>Finished<br><br>
pache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
PHP Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
PHP Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
PHP Notice: Undefined variable: ii in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 13
PHP Notice: Undefined offset: 1 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 2 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 3 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 4 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 5 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 6 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 7 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 8 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 9 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 10 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 11 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
PHP Notice: Undefined offset: 12 in E:\Program Files\Apache Group\Apache2\htdocs\blah\test2.php on line 7
>Exit code: 0 Time: 0.431
edited version:
Code: Select all
<?php
function array2str ($array, $in = 0) {
$output = '';
$tab = '';
for ($i = 0;$i < $in; $i++) {
$tab .= "\t";
}
foreach ($array as $key => $val) {
if (is_array($val)) {
$output .= "{$tab}[{$key}] => Array \n{$tab}\t(\n" . array2str($val, ($in + 1)) . "{$tab}\t)\n";
} else {
$output .= "{$tab}[{$key}] => {$val}\n";
}
}
return $output;
}
$array['stuff'] = "hi there";
$array[5] = 67;
$array['thing']['orange'] = "color";
$array[4] = 67.01;
$array['bla']['erm']['fruit'] = "color";
$serialized=serialize($array);
echo $serialized . "<br><br>";
$list = unserialize($serialized);
echo "\$array => (\n" . array2str($list, 1);
print "<br>Finished<br><br>";
?>
outputs:
Code: Select all
>E:\PHP\php.exe -q test2.php
a:5:{s:5:"stuff";s:8:"hi there";i:5;i:67;s:5:"thing";a:1:{s:6:"orange";s:5:"color";}i:4;d:67.0100000000000051159076974727213382720947265625;s:3:"bla";a:1:{s:3:"erm";a:1:{s:5:"fruit";s:5:"color";}}}<br><br>$array => (
[stuff] => hi there
[5] => 67
[thing] => Array (
[orange] => color
)
[4] => 67.01
[bla] => Array (
[erm] => Array (
[fruit] => color
)
)
<br>Finished<br><br>
>Exit code: 0 Time: 0.212
And finally...
Code: Select all
<?php
$array['stuff'] = "hi there";
$array[5] = 67;
$array['thing']['orange'] = "color";
$array[4] = 67.01;
$array['bla']['erm']['fruit'] = "color";
$serialized=serialize($array);
echo $serialized . "<br><br>";
$list = unserialize($serialized);
echo "\$array => (\n" . print_r($list, TRUE);
print "<br>Finished<br><br>";
?>
Outputs:
Code: Select all
>E:\PHP\php.exe -q test2.php
a:5:{s:5:"stuff";s:8:"hi there";i:5;i:67;s:5:"thing";a:1:{s:6:"orange";s:5:"color";}i:4;d:67.0100000000000051159076974727213382720947265625;s:3:"bla";a:1:{s:3:"erm";a:1:{s:5:"fruit";s:5:"color";}}}<br><br>$array => (
Array
(
[stuff] => hi there
[5] => 67
[thing] => Array
(
[orange] => color
)
[4] => 67.01
[bla] => Array
(
[erm] => Array
(
[fruit] => color
)
)
)
<br>Finished<br><br>
>Exit code: 0 Time: 0.221
...
Posted: Wed Jan 25, 2006 4:24 pm
by AKA Panama Jack
Man, the arrogance of the people replying here.
Jenk, I KNOW I didn't define the variable arrays as it was a QUICK hack to show what can be done. You just posted that list of ENotices to be petty.
You guys are so set on doing things ONE WAY and not even trying to open your mind and eyes to other uses and possabilities. You are coming across as very elitest in your responses.
You do know that there are other programming languages out there?
There is a javascript program that will encode and decode PHP serialized variable strings but it is not 100% complete in decoding and encoding all instances. By creating a program in PHP that does what unserialize does WITHOUT CREATING VARIABLE ELEMENTS. And for those that don't know even though you are not assigning the unserialize output to a variable name you are STILL creating variable name space for the output and it is NOT a text representation of the data encoded in the serialized string.
By creating a PHP written decoder for serialized strings that code can be used for a basis for creating similar functions on other languages that do not have an unserialize function.
Sadly, everyone that replied was so stuck up on using the unserialize function in PHP they completely IGNORED what was being asked.
It was kind of like someone asking for a PHP function to convert a two character hex value to decimal without using the HEXDEC php function and everyone posting a reply kept saying "Why? You should use HEXDEC. Detail exactly why you need this down to the last detail or we won't help you.". That is what I got for replies when I asked. Who cares if they might have been wanting to learn how something worked.
When the person asking for a hex to decimal routine could have gotten...
Code: Select all
function HexToDecimal($hex) {
$HexTable = "0123456789ABCDEF";
$hex = strtoupper($hex);
$number = StrPos($HexTable, SubStr($hex, 0, 1)) * 16;
$number = $number + StrPos($HexTable, SubStr($hex, 1, 1));
return $number;
}
Instead they got alot of reponses saying "use HEXDEC", "why use anything else", "Tell us exactly what you need it for before we will help you.".
In otherwords absolutely NO help but a ton of redicule.
In the future maybe some of you shouldn't even respond to someone asking for help if you don't know the answer or you are UNWILLING to really help. It makes you guys look bad.
Posted: Wed Jan 25, 2006 4:45 pm
by d3ad1ysp0rk
The problem, is that most of us have been taught to use the best way possible to attain any solution to a problem. That is what programming is all about after all, solving problems the best way.
When someone asks to do something which we find may be better suited another way, and will not explain why exactly they would like to do it their way, it becomes hard to really want to help.
Take for example, if someone wanted to get the sum of two text fields in javascript, then change the form action to include ?var=value after they are done that so they can use $_GET['value'] on the next page. A lot of people would be very confused on why they would want to go through all that hassle, when they could just add the values together in the PHP page and eliminate the extra variables and even the use of javascript. If they come back and get mad at us for asking, it'll be hard to want to help them anymore. Whereas if they tell us in the beginning that they want to have a prompt asking if the amount is ok before proceeding, then we'd all have an "ohhhh" moment, and help them as best we could.
We're not dicks, we're just used to programming the best way possible.
Hope that clears part of this whole situation up.
Posted: Wed Jan 25, 2006 5:07 pm
by Chris Corbyn
AKA_Panama_Jack.... you're whole attitude throughout this thread has been hostile...
I'm not going to go into the ins and outs of why you got the responses you did but as d3ad1ysp0rk says... people were simply offering the best solution as professional programmers.
I'm locking this thread since you've clearly decided it's of no purpose to you.