Web Analytics Made Easy -
StatCounter Array serialize problem - CodingForum

Announcement

Collapse
No announcement yet.

Array serialize problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Array serialize problem

    I have a file process.php that contains form data and puts it to a serialized array, which then is written to a flat file. After writing the data and unserializing the array on the other page, I need to sort the array by number values. The problem is that after serializing the sort method does not work. If someone who knows better would point out the solution, I would appreciate it. Thanks.

    Here's the process.php code:

    Code:
    <?php
    
    header ("Location: http://example.com");
    
    $Species = $_POST["kalalaji"];
    $Weight = $_POST["paino"];
    $Name = $_POST["nimi"];
    $Check = preg_match("/[A-Za-z+إؤضهنِ+, -]/i", $Weight);
    $Arr = array($Name => $Weight);
    $Ser_Arr = serialize($Arr);
    
    if($Species == "Ahven" and $Check == FALSE and $Species != "")
     {
     $File = "data.php";
     $Handle = fopen($File, "a+");
     fwrite($Handle, $Ser_Arr."\n");
     fclose($Handle);
     }
     else {
     $File = "data.php";
     $Handle = fopen($File, "r");
     fclose($Handle);
     }
     
    ?>
    and here's the php code of the page which loads the data from flat file:

    Code:
    <?php
    
    $File = file_get_contents("data.php");
    $Unser_File = unserialize($File);
    $Values = array_values($Unser_File);
    rsort($Values);
    echo $Values[0]; // The result is always the first item of the array.
    
    ?>
    Last edited by T.K.; Apr 4, 2009, 06:44 AM.

  • #2
    If you could, do a var_dump on $Unser_File and then on $Values and post what each one prints out.

    The function rsort() does not care whether the info has been serialized/deserialized in the past. All that it cares for is a valid array. What are the value types in the array? Strings? Numeric? You may have to pass the SORT_STRING flag, or possibly the SORT_LOCALE_STRING flag if you're using a different locale.

    -Shane

    Comment


    • #3
      My code creates several arrays to the data.php file. The arrays contains both numbers and strings (names). One name has its own number value. I'm sorry, the source code of process.php was in Finnish, I fixed that for clearance.

      I did a var_dump to both $Unser_File and $Values and here's the result.

      $Unser_File:

      Code:
      array(1) { ["Juhani"]=>  string(4) "3.45" }
      $Values:

      Code:
      array(1) { [0]=>  string(4) "3.45" }
      The var_dump (and sorting as I think) finds only the first array of the data file. Perhaps the problem is that there are several arrays which I must merge somehow. Can it be done from flat file? It may also be that if I could create only one array in which I write the key (name) and value (number) pairs as one element, the script would work as expected. However, that's likely beyond my PHP understanding.
      Last edited by T.K.; Apr 4, 2009, 07:23 AM.

      Comment

      Working...
      X