Jump to content

[SOLVED] Need help with serialize()


makunta

Recommended Posts

I am coding a site and have come across a situation where I need to send a serialized array to a python routine. During this coding I have been printing out results on screen as a debugging aid. Old school I know, but it works.

 

After I serialize an array I print the array to the screen as I said. During this I have noticed that the contents of the serialized array are not completely correct. Let me explain.

 

I first serialize a 10 element array and the contents are displayed correctly. I then do a 15 element array and once again the contents are correct. If I then do an array smaller then 15, say 11, then contents are correct up to element 11 but then the array fills up with the contents (12-15) from the 15 element array.

 

Here is a copy of what happens:

 

LR: RUN LOGISTIC REGRESSION ON THIS DATASET: DExcode trigger 2

(2,0)(2,0)(2,0)(2,0)(2,0)(3,1)(2,2)(4,0)(2,0)(4,1)(3,0)(3,1)(4,2)(4,1)(3,1)

LR: SEND SERIALIZED DATA TO headachestats.py:

a:15:{i:0;a:2:{i:0;s:1:"2";i:1;s:1:"0";}i:1;a:2:{i:0;s:1:"2";i:1;s:1:"0";}i:2;a:2:{i:0;s:1:"2";i:1;s:1:"0";}i:3;a:2:{i:0;s:1:"2";i:1;s:1:"0";}i:4;a:2:{i:0;s:1:"2";i:1;s:1:"0";}i:5;a:2:{i:0;s:1:"3";i:1;s:1:"1";}i:6;a:2:{i:0;s:1:"2";i:1;s:1:"2";}i:7;a:2:{i:0;s:1:"4";i:1;s:1:"0";}i:8;a:2:{i:0;s:1:"2";i:1;s:1:"0";}i:9;a:2:{i:0;s:1:"4";i:1;s:1:"1";}i:10;a:2:{i:0;s:1:"3";i:1;s:1:"0";}i:11;a:2:{i:0;s:1:"3";i:1;s:1:"1";}i:12;a:2:{i:0;s:1:"4";i:1;s:1:"2";}i:13;a:2:{i:0;s:1:"4";i:1;s:1:"1";}i:14;a:2:{i:0;s:1:"3";i:1;s:1:"1";}}

 

 

LR: RUN LOGISTIC REGRESSION ON THIS DATASET: DExcode trigger 1

(2,0)(2,1)(3,1)(3,0)(4,0)(2,2)(3,0)(3,0)(4,0)(3,0)(3,1)(2,0)(2,0)(2,0)(3,1)(4,1)(3,0)(3,2)(4,1)(4,2)(4,1)

LR: SEND SERIALIZED DATA TO headachestats.py:

a:21:{i:0;a:2:{i:0;s:1:"2";i:1;s:1:"0";}i:1;a:2:{i:0;s:1:"2";i:1;s:1:"1";}i:2;a:2:{i:0;s:1:"3";i:1;s:1:"1";}i:3;a:2:{i:0;s:1:"3";i:1;s:1:"0";}i:4;a:2:{i:0;s:1:"4";i:1;s:1:"0";}i:5;a:2:{i:0;s:1:"2";i:1;s:1:"2";}i:6;a:2:{i:0;s:1:"3";i:1;s:1:"0";}i:7;a:2:{i:0;s:1:"3";i:1;s:1:"0";}i:8;a:2:{i:0;s:1:"4";i:1;s:1:"0";}i:9;a:2:{i:0;s:1:"3";i:1;s:1:"0";}i:10;a:2:{i:0;s:1:"3";i:1;s:1:"1";}i:11;a:2:{i:0;s:1:"2";i:1;s:1:"0";}i:12;a:2:{i:0;s:1:"2";i:1;s:1:"0";}i:13;a:2:{i:0;s:1:"2";i:1;s:1:"0";}i:14;a:2:{i:0;s:1:"3";i:1;s:1:"1";}i:15;a:2:{i:0;s:1:"4";i:1;s:1:"1";}i:16;a:2:{i:0;s:1:"3";i:1;s:1:"0";}i:17;a:2:{i:0;s:1:"3";i:1;s:1:"2";}i:18;a:2:{i:0;s:1:"4";i:1;s:1:"1";}i:19;a:2:{i:0;s:1:"4";i:1;s:1:"2";}i:20;a:2:{i:0;s:1:"4";i:1;s:1:"1";}}

 

 

LR: RUN LOGISTIC REGRESSION ON THIS DATASET: Got very hungry at some point in past 24 hours.

(4,0)(4,0)(4,1)(4,2)(4,0)(4,1)(4,2)(4,0)(4,0)(4,1)(4,0)(4,2)(4,1)(4,1)

LR: SEND SERIALIZED DATA TO headachestats.py:

a:21:{i:0;a:2:{i:0;s:1:"4";i:1;s:1:"0";}i:1;a:2:{i:0;s:1:"4";i:1;s:1:"0";}i:2;a:2:{i:0;s:1:"4";i:1;s:1:"1";}i:3;a:2:{i:0;s:1:"4";i:1;s:1:"2";}i:4;a:2:{i:0;s:1:"4";i:1;s:1:"0";}i:5;a:2:{i:0;s:1:"4";i:1;s:1:"1";}i:6;a:2:{i:0;s:1:"4";i:1;s:1:"2";}i:7;a:2:{i:0;s:1:"4";i:1;s:1:"0";}i:8;a:2:{i:0;s:1:"4";i:1;s:1:"0";}i:9;a:2:{i:0;s:1:"4";i:1;s:1:"1";}i:10;a:2:{i:0;s:1:"4";i:1;s:1:"0";}i:11;a:2:{i:0;s:1:"4";i:1;s:1:"2";}i:12;a:2:{i:0;s:1:"4";i:1;s:1:"1";}i:13;a:2:{i:0;s:1:"4";i:1;s:1:"1";}i:14;a:2:{i:0;s:1:"3";i:1;s:1:"1";}i:15;a:2:{i:0;s:1:"4";i:1;s:1:"1";}i:16;a:2:{i:0;s:1:"3";i:1;s:1:"0";}i:17;a:2:{i:0;s:1:"3";i:1;s:1:"2";}i:18;a:2:{i:0;s:1:"4";i:1;s:1:"1";}i:19;a:2:{i:0;s:1:"4";i:1;s:1:"2";}i:20;a:2:{i:0;s:1:"4";i:1;s:1:"1";}}

 

I use this command to serialize the array:

$sarr = serialize($logRegArray); // $sarr stands for serialized-array

and this to print it:

print_r($sarr);

 

A full printout can be seen here:

http://hpashler.webfactional.com/analyze-trigger160409.php

 

What I need to do is send a number of 2-dimensional arrays to a python routine. I need to send each array separately and I need to serialize the array before I send it.

 

Do I need to reset the serialized array or what am I doing wrong?

Any help in this situation would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/154374-solved-need-help-with-serialize/
Share on other sites

So, have you examined what exactly is in $logRegArray? Perhaps it is being reused as is and the left over elements on the end are still present? What is your code that uses $logRegArray?

 

You are correct, I have not emptied the array before reusing it. This is the code I am using:

 

$i = 0;
print "LR: RUN LOGISTIC REGRESSION USER DEFINED TRIGGERS:<br />";		
while ($row = sql_fetch_array($result)) {
    //store trigger question
    $usertrigger[$i] = $row['selfquestion'];
    $finalLogRegArray[$triggerIndex][0] = $usertrigger[$i];
    $triggerId = $row['id'];
    //get all days in database matching the user defined trigger from the 'daily_trigger' table
    $lcquery2 = "select ddate, answerno from daily_trigger where questionid='$triggerId' and answerno>1 and email='$lcemail' order by ddate desc";
    $result2 = sql_query($lcquery2, $link);
    $j = 0; 
    while ($row2 = sql_fetch_array($result2)) {
        //see if there is headache data for the day after the trigger date
        $dateT = $row2['ddate'];
        $triggerData = $row2['answerno'];
        //calculate the day after the trigger date
        $dtT = split("-", $row2['ddate']);
        $CDateT = userdate("U", mktime(0, 0, 0, $dtT[1], $dtT[2], $dtT[0]));
        $NDateT = adddate($CDateT, "d", + 1);
        $NDateT = gmdate("Y-m-d", $NDateT);
        //see if there is any headache data in the 'daily' table
        $lcquery3 = "select migraine from daily where email='$lcemail' and ddate='$NDateT'";
        $result3 = sql_query($lcquery3, $link);
        //if there is headache data then save results
        if (sql_num_rows($result3) > 0) {
            $row3 = sql_fetch_array($result3);
            $logRegArray[$j][0] = $triggerData;
            $headacheData = $row3["migraine"];
            $logRegArray[$j][1] = $headacheData; 
            $j++;
        }        
    } 

print "LR: RUN LOGISTIC REGRESSION ON THIS DATASET: " . $usertrigger[$i];
echo '<pre>' . var_export($logRegArray,true) . '</pre>';
	 //send dataset to the python logistic regression routine
	 $sarr = serialize($logRegArray);  // $sarr stands for serialized-array 
	 print "LR: SEND SERIALIZED DATA TO headachestats.py: <br />";
echo '<pre>' . var_export($sarr,true) . '</pre>';
     //store results from logistic regression
	 $hstatsoutput = exec("python headachestats.py '".$sarr."'");
	 $oarr = split(",",$hstatsoutput); //$oarr stands for output array
	 if ($oarr[1] == -1){
		 //not enough data so do next trigger
		 $finalLogRegArray[$triggerIndex][1] = 0; 
		 $finalLogRegArray[$triggerIndex][2] = 0;
	 } else {
		 //$oarr[*] may need to be converted to numeric types
		 $finalLogRegArray[$triggerIndex][1] = $oarr[0]; 
		 $finalLogRegArray[$triggerIndex][2] = $oarr[1];
	 }
	$triggerIndex++;   
    $i++;  
}

 

Please show us the results of:

<?php
echo '<pre>' . var_export($logRegArray,true) . '</pre>';
$sarr = serialize($logRegArray);
echo '<pre>' . var_export($sarr,true) . '</pre>';

 

Thank you for the debugging tip. You can see the results here:

 

http://hpashler.webfactional.com/analyze-trigger160409.php

 

How do I clear out the array so I can reuse it or is there a better way of doing things?

 

Thanks for your help.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.