In part 5 of my Web Design and Programming tutorial I will cover the following topics:
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>
Hi, i’m desperately trying to use php with an array with the intention to have php call information from the array and display relevant text/media.
For my project I am trying to do the following:
A.. Write an array that contains the following:
a. Video Title
b. Image (thumbnail from video to be hosted locally)
C. Video Link (www.youtube.com for example)
d. Description (text)
I wish to have a page with several videos counting back from the ‘total’ value of entries in the array, and for it to display:
a, b and d, – all acting as a link to a new page that contains a,c, and d.
I had considered it being created dyanmically through the clicking, but for now i’m fine to have a static page with the entries in.
How on earth do I go about this?
My test array is:
array
(
“Man argues with spitting goat”,
“Making Handmade natural and organic soaps”,
“Fainting Goats”,
“The Talking Goat”,
“Fainting Goats 2″,
“Google Goats”
),
“Video”=>array
(
“http://www.youtube.com/watch?v=Dp0Bt2cbcc8&feature=fvst”,
“http://www.youtube.com/watch?v=3RjehTbgwXQ&feature=fvst”,
“http://www.youtube.com/watch?v=we9_CdNPuJg”,
“http://www.youtube.com/watch?v=x0S5zS8jSE4″,
“http://www.youtube.com/watch?v=f_3Utmj4RPU”,
“http://www.youtube.com/watch?v=Jw_XI0WVZBg”
),
“Description”=>array
(
“Vid 1″,
“Vid 2″,
“Vid 3″,
“Vid 4″,
“Vid 5″,
“Vid 6″
)
);
$Total = 5;
?>
I will do a tutorial on this as soon as I’m done with PHP security. Everything you ever wanted to know about web site scraping. Thanks