In part 5 of my Web Design and Programming tutorial I will cover the following topics:
- How to turn a string into an array
- How to turn an array into a string
- How to sort an array
- How to pull data from a file and store it in an array
And, I’ll also go over a bunch of other functions specific to arrays. If you missed part 1 of this tutorial it is available here Web Design and Programming.
All of the code in this tutorial will follow the video. Use it in any way that you would like.
If you have any questions or comments leave them below and I’ll answer them.
Code from the Video
<html>
<head>
<title><?php echo “Arrays”;?></title>
</head>
<body>
<?php
# An array is just a big box containing many similar boxes
# This information is similar to each other
# All the the information or values have a coresponding key or label associated with it
# An array can contain any combination of numbers, strings or other arrays
# Create basic array with key value pairs
/* $myInfo = array(“Name” => “Derek”, “Street” => “123 Main St”, “City” => “Pittsburgh”);
# Get the value by calling for the key associated with it
echo “My name is “, $myInfo[“Name”], “<br /><br />”;
$moreInfo = array(“State” => “PA”, “Age” => 35);
# Merge 2 arrays into one
$myInfo = array_merge($myInfo, $moreInfo);
# Iterate through the array with the foreach looping device
foreach( $myInfo as $key => $value)
{
echo $key, ” “, $value, “<br />”;
}
echo “<br /><br />”;
# Search for a key in an array
if(array_key_exists(“Name”, $myInfo)) echo “The name stored is “, $myInfo[“Name”];
echo “<br /><br />”;
# Search for a value in an array
$citySearch = array_search(“Pittsburgh”, $myInfo);
echo “The key for the city is “, $citySearch;
echo “<br /><br />”;
print_r (array_keys($myInfo)); # Short cut to provide the arrays keys
echo “<br /><br />”;
print_r (array_values($myInfo)); # Short cut to provide the arrays keys & values
echo “<br /><br />”;
# Create a Multidimensional Array
$customer1 = array(“Name” => “Derek”, “Street” => “123 Main St”, “City” => “Pittsburgh”);
$customer2 = array(“Name” => “Sally”, “Street” => “213 Grant St”, “City” => “Pittsburgh”);
$customers = array($customer1,$customer2);
print_r(array_values($customers));
echo “<br /><br />”;
foreach( $customers as $key)
{
foreach( $key as $key2 => $value)
{
echo $key2, ” “, $value, “<br />”;
}
} */
$countryStr = “Cuba,Spain,India,France,Italy”;
$randCountry = explode(“,”, $countryStr);
echo $randCountry[0], ” “, $randCountry[1], “<br /><br />”;
$countryStr2 = implode(“,”, $randCountry);
echo $countryStr2, “<br /><br />”;
if(in_array(“India”, $randCountry)) echo “India is in the list”;
echo “<br /><br />”;
print_r(array_reverse($randCountry,true));
echo “<br /><br />”;
sort($randCountry, SORT_STRING);
print_r($randCountry);
echo “<br /><br />”;
$countArray = range(0,50);
foreach($countArray as $printNum)
{
echo $printNum, “, “;
}
echo “<br /><br />”;
echo count($countArray);
echo “<br /><br />”;
$customers = file(“customers.txt”);
foreach($customers as $customer)
{
list($name,$street,$city,$state) = explode(“,”,$customer);
$state = trim($state);
echo “$name $street $city $state”;
echo “<br /><br />”;
}
?>
</body>
</html>