Category search : php...

(3 results found)

Passing Serialized Arrays Between PHP Pages

Fri Feb 12 05:58:43 2010

Passing arrays between pages can be a useful thing to know, particularly when you feel data is a little too long to stick inside a url and use $_GET[]. That is not to say that this is the best way of doing it. I am quite sure that there are many coders out there just shouting about a better method, or about the does and don'ts of PHP, but this, as I say, is good to know. So here goes...

First off, we need an array to pass. I will be using the one below as an example.

$arrayToSerialize = array("key1"=>"value1","key2"=>"value2","key3"=>"value3");
As you can see, it is an associative array with keys and values. To serialized the array simply call the serialized() method and pass in the array name, as in the following example. Note that I have used the variable $serializedArray to store the serialized data.

$serializedArray = serialize($arrayToSerialize);
Now we create a form to send the serialized data on a submit button click as follows.

<form action="unserialize.php" method="post">

      <input type="hidden" id="serializedArray" name="serializedArray" 
         value='<?php echo $serializedArray;?>'/>

      <input type="submit" value="Continue;">
The action of the form above points to a file we will create next called unserialize.php. The method is set to post as we will be sending the data. Inside the form, there is a hidden input which will store and pass the data on to the unserialize.php file. The name of the hidden input is set to 'serializedArray' which we will need to remember for later. Lastly, inside the form also resides the submit button that will trigger the form and send the data.

Next we need to create a file called unserialize.php as we mentioned earlier. Inside that file we write the following.

$serializedArray = $_REQUEST["serializedArray"]; 
$unserializedArray = unserialize(stripslashes($serializedArray));  
The code above captures the serialized data in a variable again called $serializedArray. Notice that the name of the hidden input inside serialize.php is placed inside a server $_REQUEST[] to retrived the data. In the line after that the data is unserialized with the unserialize() method call, passing in the $serializedArray variable. The unserialized data is them stored in another variable called $unserializedArray. The $unserializedArrayvariable can now be used as any other array. If you use print_r($unserializedArray), you will notice the results shown below are the same as the original array passed from serialized.php.

Results of print_r($unserializedArray) :

Array ( [key1] => value1 [key2] => value2 [key3] => value3 )

I have put together a quick example which you can download from the link below.

Download Serialize Array Example

Remove Spaces in PHP Strings

Mon Oct 26 01:07:57 2009

I'm still very much at the beginning of learning PHP and while programming this blog, I came across a wonderful PHP function that can be used to remove/replace spaces (amongst other things) in strings. I know it will come in handy for someone. It's called str_replace(); The syntax is as follows:

str_replace(item to be replaced, item to replace with, string to commit this function);
//creating a string held inside a variable.
$origString = "This is the original string.";
//display the string.
echo $origString;

//replace " " width "". In other words, replace spaces with no spaces.
$newString = str_replace (" ", "", $origString);
//display the new string.
echo $newString;


Counting PHP Array Repetitions

Sat Oct 17 04:07:54 2009

The ability to check an array in php and see if the items it contains reoccur somewhere else inside the array is something that comes in handy once in a while. If for example, you have a list of dates and you wanted to count how many times the same date reoccurs in the array and display the results, this is what you would do.

 $arrayToCount = array("1981","1973","2010","1973","1981","1973");

 foreach( $arrayToCount as $val ) {
   if ( array_key_exists( $val, $arrayCounter) ) { 
   else { 
      $arrayCounter[$val] = 1;
 while( $element = each( $arrayCounter ) ){
 echo $element[ 'key' ].' ('.$element[ 'value' ].')';

As is, the results of this would look like this

1981 (2)
1973 (3)
2010 (1)

Here's what's happening. Obviously, we have an array containing dates that we want to count shown on line 1. We use a foreach loop to run the contents of the loop for however many dates are contained inside the array. An if/else statement is used on line 4 through 9, which uses the PHP function array_key_exists()(line 4.), which checks to see if $val (the date being checked) matches a value previously looped through. If this function returns true the value inside $arrayCounter gets pushed up by 1 or ++ each time (see line 5). If the array_key_exists() on line 4 returns false, or in other words, no match is found, then the value 1 gets added to a new place in the $arrayCounter array. We then use a while statement to display each of the dates and their count values stored inside the associative array $arrayCounter(lines 12 ~ 14)

And that's about it. If you are new to PHP, I would recommend drilling this kinds of routine into your head and you'll get your head around it before you know it.

Most recent posts

  1. Flashcards Maker - the app the helps you learn in a flash
  2. Convert Font Suitcase to TTF in 3 Easy Steps
  3. Rain or Shine - Weather Forecast Available Now!
  4. Getting around Apple's silly rules for iOS 10
  5. Localising App Names for iOS and Android

Search site:

Apps by

Click the app icons to see more info