-
-
Save otengkwame/e617fd3cab7cd86376824c1ca0bffb63 to your computer and use it in GitHub Desktop.
Week 3 - Day 5 - Playing with Arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Playing with Arrays | |
* List of functions: http://us2.php.net/manual/en/ref.array.php | |
**/ | |
// Creating an array | |
$array = array('value1', 2, 'value3', 4.5); | |
// Remember an array starts at position 0 | |
// Printing the contents of an array | |
print_r($array); | |
// That will give us something like: | |
// Array ( [0] => value1 [1] => 2 [2] => value3 [3] => 4.5 ) | |
// As you can see it begins at 0 and not 1 | |
// How do access a specific value? | |
echo $array[2]; // returns value3 | |
// Looping through an array | |
foreach($array AS $value){ | |
echo $value . '<br />'; | |
// will echo value1 then 2, then value3 and then 4.5 separated by a break | |
} | |
// How many items are in an array? | |
echo count($array); // 4 | |
// How do I store data under a string? | |
$newArray = array('key' => 'value', 'key2' => 'value2'); | |
// Now how would I get 'value2' to show on my screen? | |
echo $newArray['key2']; | |
// How would I replace value2 with something else? | |
$newArray['key2'] = 'something else'; | |
// Checking if an item exists in an array | |
$names = array('Marcus', 'Tom', 'Jerry'); | |
if( in_array('Jeff', $names) ){ | |
echo 'Jeff is in the array!'; | |
}else { | |
echo 'Jeff is not in the array!'; | |
// he is not so this will be shown | |
} | |
// Get rid of the last item on an array | |
$lastName = array_pop($names); // $lastName contains 'Jerry' | |
// using print_r on $names will only show you Marcus and Tom now | |
// Add an item to the back of an array | |
array_push($names, 'Jerry'); // yay Jerry is back! | |
// or... | |
$names[] = 'Jeff'; // Jeff is in our array now! | |
// Our array is now: Marcus, Tom, Jerry, Jeff | |
// Reverse the order of the array | |
$reversed = array_reverse($names); // Jeff, Jerry, Tom, Marcus | |
// Sort a bunch of numbers | |
$numbers = array(7, 4, 1, 3, 6); | |
sort($numbers); | |
print_r($numbers); // 1, 3, 4, 6, 7 | |
rsort($numbers); // High to low! | |
print_r($numbers); // 7, 6, 4, 3, 1 | |
// How about sorting names with mixed cases? | |
$newNames = array('Cheryl', 'Bob', 'bernie', 'Amanda'); | |
natcasesort($newNames); | |
print_r($newNames); // Amanda, bernie, Bob, Cheryl | |
// Let's shuffle an array! | |
shuffle($newNames); | |
print_r($newNames); // could return: Bob, Amanda, Cheryl, bernie | |
// Calculate the sum of all numbers in an array | |
$numberList = array(1, 8, 13, 6, 5); // why do 1 + 8 + 13 + 6 + 5 | |
echo array_sum($numberList); // 33 | |
// Merge multiple arrays? | |
$fruits = array('a' => 'apple', 'b' => 'banana', 'c' => 'canteloupe'); | |
$veggies = array('z' => 'zucchini', 'y' => 'yam', 's' => 'shallot'); | |
$fruitsAndVeggies = array_merge($fruits, $veggies); | |
print_r($fruitsAndVeggies); | |
// a => apple, b => banana, c => canteloupe, z => zucchini, y => yam, s => shallot | |
// Apply a function to an array | |
$lowercased = array('these', 'words', 'are', 'all', 'lowercased'); | |
$uppercased = array_map('strtoupper', $lowercased); | |
print_r($uppercased); // THESE, WORDS, ARE, ALL, LOWERCASED | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment