Pierre-luc
Pierre-luc

Reputation: 33

Why serialize() have terrible performance php mysql

I try to store multi array in mysql. serialize() and unserialize() work fine but loading time of page increase significantly... like very long and very bad even with small array with 20-50 objects.

Is there a way to avoid this ?

My issue is I have a long code with function but dont remember the array after the code :

for exemple

  function {

  // There is very long code here with foreach, loop and all that crap.

  // Working and displaying array

  $finalarray = ($array["stuff"]);
  print_r($finalarray);


  }

  // Here after function the variable is forgotten and print_r($finalarray) dont work anymore

Why the variable is unset after function because this is why I want to store it in mysql in the first place maybe i can avoid that by just remember the array.

I did not put all the code because its huge but if you need anymore information about my issue just let me know.

Thanks !

Upvotes: 0

Views: 59

Answers (1)

Oleg Dubas
Oleg Dubas

Reputation: 2348

What you are asking about is called "variable visibility scope". In PHP if you define a variable within a function, it will NOT be visible after the function ended.

To make the variable you create available after the function execution for the outside code to access, you have to declare it global, like this:

function f() 
{
    global $finalarray;
    ...
    $finalarray = ......;
}

f();
print_r($finalarray);

Another way to do it is to make the function return the array. Like this:

function f() 
{
    $finalarray = ......;  // local variable
    return $finalarray;    // return by value
}

$finalarray = f();     // becomes a global variable
print_r($finalarray);

Upvotes: 2

Related Questions