1. Array
2. File
3. Pattern matching
4. String
1.1. Creating array
Simple array:
$array = array("one", "two", "three");
Multidimensional array:
$row_0 = array(1, 2, 3);
$row_1 = array(4, 5, 6);
$row_2 = array(7, 8, 9);
$array = array($row_0, $row_1, $row_2);
Hash:
$array = array(
"first" => "one",
"second" => "two",
"third" => "three"
);
Hash with multiple values:
$array = array(
"first" => array("one", "uno"),
"second" => array("two", "due"),
"third" => array("three", "tre")
);
1.2. Adding value to array
Add at index 4:
$array[4] = "four";
Add at the end of the array:
$array[] = "end";
1.3. Accessing value in array
Simple array:
$value = $array[0];
Multidimensional array:
$value = $array[$row][$colomn];
Hash:
$value = $array["second"];
Hash with multiple values:
$row = $array["second"];
$value = $array["second"][$language];
Subset of values:
$subset = array_slice($array, $offset, $length);
1.4. Walking through an array
Simple array:
for($array as $value)
{
print "$value\n";
}
or
for($i = 0; $i < count($array); $i++)
{
print "$value[$i]\n";
}
or
echo $array;
Hash:
foreach($array as $key => $value)
{
print "$key: $value\n";
}
Note that if one of the elements is not into the array, there will be an error in the foreach loop.
Hash with multiple values:
foreach($array as $key => $values)
{
foreach($values as $value)
{
print "$value\n";
}
}
Note that if one of the elements is not into the array, there will be an error in the foreach loop.
1.5. Getting length of an array
count($array);
1.6. Checking if is an array
if(is_array($x) == true)
{
echo "is an array";
}
else
{
echo "is not an array";
}
1.7. Checking for key existence
$key = "third";
if(isset($array[$key]))
{
print "$key is present\n";
}
1.8. Checking for value existence
Simple array:
$value = "three";
if(in_array($array, $value))
{
print "$value is present\n";
}
1.9. Finding position of a value
$value = "three";
$position = array_search($array, $value);
if($position != false)
{
print "$value is in position $position\n";
}
1.10. Iterator functions
Returns element pointed at by the iterator:
$current = current($array);
Moves iterator to first element and returns it:
$first = reset($array);
Moves iterator to next elment and returns it:
$next = next($array);
Moves iterator to previous element and returns it:
$previous = prev($array);
Moves iterator to last elment and returns it:
$last = end($array);
Returns key and value of the current element and moves iterator to next element:
list($key, $value) = each($array);
Returns key of current element:
$key = key($array);
1.11. Deleting element from array
One element:
unset($array[3]);
unset($array["third"]);
Multiple noncontiguos elements:
unset($array[3], $array[5]);
unset($array["third"], $array["fifth"]);
Multiple contiguous elements:
array_splice($array, $offset, $length);
1.12. Splitting array
list($first, $second, $third) = $array;
1.13. Appending array
$array = array_merge($array1, $array2);
1.14. Sorting array
By values (alphabetically), then reassign indexes starting with 0:
$numbers = array("one, "two", "three");
[0]: "one"
[1]: "two"
[2]: "three"
sort($array);
[0]: "one"
[1]: "three"
[2]: "two"
Same as before, but sort numerically:
sort($array, SORT_NUMERIC);
If the array is a hash:
By key:
ksort($hash);
By key, reverse order:
krsort($hash);
By key, with user defined function:
function mySortFunction ($a, $b)
{
if($a == $b)
return 0;
return ($a > $b) ? -1 : 1;
}
uksort($hash, mySortFunction);
By values:
asort($hash);
By values, reverse order:
arsort($hash);
By values, with user defined function:
function mySortFunction ($a, $b)
{
if($a == $b)
return 0;
return ($a > $b) ? -1 : 1;
}
uasort($hash);
1.15. Removing duplicate values from array
$array_unique = array_unique($array);
1.16. Comparing arrays
Union:
$union = array_unique(array_merge($array1, $array2));
Intersection:
$intersection = array_intersection($array1, $array2);
Difference:
$difference = array_diff($array1, $array2);
Symmetric difference:
$symmetric_difference = array_merge(array_diff($array1, $array2), array_diff($array2, $array1));
2.1. Reading file line by line
$file = 'myfile.txt';
$fh = fopen($file, 'r') or die($php_errormsg);
while(!feof($fh))
{
$line = rtrim(fgets($fh, 1048576));
}
fclose($fh) or die($php_errormsg);
3.1. Replace
Replace all double blanks with
$line = ereg_replace(' ', ' ', $line);
4.1. Controlling case
Convert to uppercase:
$uppercase = strtoupper($string);
Convert to lowercase:
$lowercase = strtolower($string);
|