Removing quotes - a step by step example of manipulating array data

This article discusses some techniques of manipulating data in an array. To keep it simple, let's start by looking how we can remove quotes from a variable. To remove a character, we can replace it with nothing using str_replace() function.

$a = "'pineapple'";
$b = '"guava"';
print "$a $b\n";
$a = str_replace("'","",$a);
$b = str_replace('"','',$b);
print "$a $b\n";

This results in:

'pineapple' "guava"
pineapple guava

Suppose we need to be perform this operation on every element of the array. The logical place to start would be to use a foreach loop.

print "original\n";
$fruits = array("'apple'","'banana'","'nectar'");
print_r($fruits);

print "Using foreach\n";
foreach ($fruits as $fruit) { $fruit = str_replace("'","",$fruit); }
print_r($fruits);

Unfortunately this does not work as the variable $fruit is temporary variable which holds a copy of the given element. Modifying the value of this$fruit does not modify the value of the desired element in the array.

original
Array
(
    [0] => 'apple'
    [1] => 'banana'
    [2] => 'nectar'
)
Using foreach
Array
(
    [0] => 'apple'
    [1] => 'banana'
    [2] => 'nectar'
)

Let's try this with a for loop.

print "original\n";
$fruits = array("'apple'","'banana'","'nectar'");
print_r($fruits);

print "Using for loop\n";
for ($i = 0; $i < count($fruits); $i++) {
  $fruits[$i] = str_replace("'","",$fruits[$i]);
}
print_r($fruits);

Pretty self-explanatory. Loop through the array, extract each element, remove quotes and replace the original element in the array with this one.

original
Array
(
    [0] => 'apple'
    [1] => 'banana'
    [2] => 'nectar'
)
Using for loop
Array
(
    [0] => apple
    [1] => banana
    [2] => nectar
)

Very often we are interested in performing more complex manipulations than simply removing codes. In such cases, it is a good idea to delegate the work to a function.

function strip_single_quotes($value)
{
  return str_replace("'","",$value);
}
print "original\n";
$fruits = array("'apple'","'banana'","'nectar'");
print_r($fruits);

print "Using for loop by value\n";
for ($i = 0; $i < count($fruits); $i++) {
  $fruits[$i] = strip_single_quotes($fruits[$i]);
}
print_r($fruits);

strip_single_quotes() is called by value, meaning that strip_single_quotes() receives a copy of the the array element. strip_single_quotes() removes quotes from this copy and returns the modified copy. This copy then replaces the array element. The result of this program:

original
Array
(
    [0] => 'apple'
    [1] => 'banana'
    [2] => 'nectar'
)
Using for loop by value
Array
(
    [0] => apple
    [1] => banana
    [2] => nectar
)

Another way to accomplish the same is by sending a reference rather than a copy to the function.

function remove_single_quotes(&$value)
{
  $value = str_replace("'","",$value);
}

print "original\n";
$fruits = array("'apple'","'banana'","'nectar'");
print_r($fruits);

print "Using for loop by reference\n";
for ($i = 0; $i < count($fruits); $i++) {
  remove_single_quotes($fruits[$i]);
}
print_r($fruits);

Here we send a reference (address of) the array element. Note the &$ in remove_single_quotes(). As remove_single_quotes() operates on the original element, there is no need to return a value. The output:

original
Array
(
    [0] => 'apple'
    [1] => 'banana'
    [2] => 'nectar'
)
Using for loop by reference
Array
(
    [0] => apple
    [1] => banana
    [2] => nectar
)

A nicer way to accomplish the same is by using array_walk() instead of a loop. array_walk() iterates through an array apply a function to each element of the array.

function remove_single_quotes(&$value)
{
  $value = str_replace("'","",$value);
}

print "original\n";
$fruits = array("'apple'","'banana'","'nectar'");
print_r($fruits);

print "Using array_walk\n";
array_walk($fruits,'remove_single_quotes');
print_r($fruits);

The output:

original
Array
(
    [0] => 'apple'
    [1] => 'banana'
    [2] => 'nectar'
)
Using array_walk
Array
(
    [0] => apple
    [1] => banana
    [2] => nectar
)

Comments?