Archive for category PHP
How to convert an array or object into a string in php
Posted by Colin Jensen in PHP, Tips and Tricks on August 9th, 2009
I am asked “How do I convert an array or object into a string” very frequently so I thought I would put a quick little tutorial together explaining the various different methods.
Converting an array OR object into a string(BEST METHOD)
We are going to use the functions serialize and unserialize.
These functions are very easy to use, for example converting an array or object you would do the following:
$string = serialize($objectorarray);
Now to convert the string back into an object or array we would do:
$objectorarray = unserialize($string);
It really is as simple as that, however sometimes the serialize function doesn’t do a good job at escaping characters so to get around this we do the following:
$string =base64_encode(serialize($objectorarray)); // This converts into a string$objectorarray = unserialize( base64_decode($string)); // This converts a string into an array or object
Converting an array into a string (OLD FASHIONED METHOD).
Implode and Explode can be used to convert an array.
Look at my example below.
$myarray[] = 'Apple';$myarray[] = 'Orange';
$string = implode('|',$myarray);
echo $string; // This would return Apple|Orange
Using the example above, the implode function takes two parameters. First our separator and second our array.
To now convert the string BACK into an array we use explode. See the example below.
$string = 'Apple|Orange';$myarray = explode('|',$string);
print_r($myarray);
The explode function takes pretty much the same parameters as implode except it looks for a string rather then an array.
Why would you want to convert an array or object into a string?
When saving vast amounts of data into a mysql table or even a text file, all you would need to do using the method above is save the string into the file or into a table row. Very useful if you have plenty of dynamic data… No need to create lots of column names when you can save the object or array into a single row!
Tutorial – How to use arrays in php
Posted by Colin Jensen in PHP on June 26th, 2008
Using arrays in PHP can be a very powerful technique of handling data.
If you are not familiar with arrays in PHP then use simple code!
Example of simple code.
$fruit['apples'] = 'I like apples';
$fruit['oranges'] = 'I like oranges';
$fruit['banana'] = 'I like bananas';
Example of advanced code.
$fruit = array("apples" => 'I like apples', "oranges" => 'I like oranges', "banana" => 'I like bananas');
Using advanced code saves lines in the source code but if you are not familiar with arrays it could be harder to troubleshoot if you get parse errors. Once you feel confident with arrays then I would recommend using advanced code.
Reading from arrays.
Arrays are very simple to read from. Using our example code above, to see the value of Apples we would use
echo $fruit['apples'];
this would return: I like apples
Looping through arrays
Looping through arrays is easy too, but this is where people usually get stuck. Just look at it from a logical point of view. We are going to use the foreach function to go through the array.
foreach($fruit as $key => $value) {
echo $key . ' : ' . $value . '<br />';
}
This would return:
apples : I like apples
oranges: I like oranges
banana: I like bananas
Using the foreach function above, think of $key as the name and $value as the information we assigned.
If you would like any more information regarding arrays then please leave a comment or visit the PHP manual by clicking here