Web Design and Programming Pt 5 PHP Arrays

Go Daddy Grid HostingIncrease Website SpeedIn 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>

5 Responses to “Web Design and Programming Pt 5 PHP Arrays”

  1. Remainder says:

    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;

    ?>

  2. Tommy L says:

    First I must say THANK YOU SO MUCH!!!!
    I am enjoying learning all of this great info and hope to make couple of different WordPress themes.

    (using WAMP, firefox, i.e., and Notepad++)
    I am working my way through this tutorial and can’t seem to fix a couple of things. I have checked my code, retyped my code, changed my code, added and subtracted code, and still can’t fix the problems:

    1) This is the code I typed that will NOT show the “India is in the list” on the webpage even through multiple variations:

    if(in_array(“India”, $randCountry))
    {
    echo “India is in the list”;
    echo “”;
    }
    else
    {
    echo “”;
    }
    // Does NOT work

    2) This next section will not put Cuba in alphabetical order. Whatever is first in the list will sort to last. I have tried many variations and cannot get the first item to be included in the sort order correctly.
    /*

    “Derek”, “Street” => “123 Main”, “City” => “Pittsburgh”);
    // key is Name, Street, City, ect

    echo “My name is “, $myInfo[“Name”], “”;

    $moreInfo = array(“State” => “PA”, “Age” => 35);

    $myInfo = array_merge($myInfo, $moreInfo);

    foreach( $myInfo as $key => $value)
    {
    echo $key, “: “, $value, “”;
    }
    echo “1“;
    echo “”;

    if(array_key_exists(“Name”, $myInfo)) echo “The name stored is “, $myInfo[“Name”];
    echo “”;

    $citySearch = array_search(“Pittsburgh”, $myInfo);
    echo “The key for the city is “, $citySearch;
    echo “”;

    print_r(array_keys($myInfo));
    echo “”;

    print_r(array_values($myInfo));
    echo “”;
    echo “2“;
    $idnumber = 1;

    $customer1 = array(“ID” => $idnumber++, “Name” => “Derek”, “Street” => “123 Main”, “City” => “Pittsburgh”);
    $customer2 = array(“ID” => $idnumber++, “Name” => “Sally”, “Street” => “23 Main”, “City” => “Pittsburgh”);
    $customer3 = array(“ID” => $idnumber++, “Name” => “Tommy”, “Street” => “10 Stagecoach NW LN”, “City” => “Sheridan”);

    $customers = array($customer1, $customer2, $customer3);
    print_r(array_values($customers));

    echo “”;

    foreach($customers as $key)
    {
    echo “”;
    foreach($key as $key2 => $value)
    {
    echo $key2, “: “, $value, “”;
    }
    } */

    $countryStr = “Iglu, Spain, Cuba, India, France, Italy, Germany”;
    $randCountry = explode(“,”, $countryStr);
    echo $randCountry[0], ” “, $randCountry[1], “”;
    // the 0 and 1 are the index numbers for the array
    echo “3“;
    $countryStr2 = implode(“,”, $randCountry);
    echo $countryStr2, “”;
    echo $countryStr, “”;
    echo $randCountry[0], ” “, $randCountry[1], “”;
    // the 0 and 1 are the index numbers for the array
    print_r(array_values($randCountry));
    echo “”;

    if(in_array(“India”, $randCountry))
    {
    echo “India is in the list”;
    echo “”;
    }
    else
    {
    echo “”;
    }
    // Does NOT work

    print_r(array_reverse($randCountry,true));
    echo “”;

    sort($randCountry, SORT_STRING);
    // SORT_STRING (alphabetical sorting), SORT_NUMERIC (string of numbers), SORT_REGULAR (sorts ASCI codes)
    print_r($randCountry);
    echo “”;
    ?>

    */
    Sorry, I included all of the text as I am unsure of any reference errors.
    Can you see where I am messing up?
    Thank you again SO MUCH!

    Also, Do you cover prioritizing links in WordPress in one of your videos?

Leave a Reply

Your email address will not be published.

Google+