Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Codecademy;
- // create Associative Arrays in key-value pairs
- $php_array = array("language" => "PHP", "creator" => "Rasmus Lerdorf", "year_created" => 1995, "filename_extensions" => [".php", ".phtml", ".php3", ".php4", ".php5", ".php7", ".phps", ".php-s", ".pht", ".phar"]);
- $giraffe_foods = ["dip"=>"guacamole", "chips"=>"corn", "entree"=>"grilled chicken"];
- $impala_foods = ["dessert"=>"cookies", "vegetable"=>"asparagus", "side"=>"mashed potatoes"];
- $rat_foods = ["dip"=>"mashed earth worms", "entree"=>"trash pizza", "dessert"=>"sugar cubes", "drink"=>"lemon water"];
- $potluck = $giraffe_foods + $impala_foods;
- print_r($potluck);
- $potluck += $rat_foods; // $$rat_foods has duplicated keys with other arrays, so only unique keys will be added
- print_r($potluck);
- $doge_meme = ["top_text" => "Such Python", "bottom_text" => "Very language. Wow.", "img" => "very-cute-dog.jpg", "description" => "An adorable doge looks confused."];
- $bad_meme = ["top_text" => "i don't know", "bottom_text" => "i can't think of anything", "img" => "very-fat-cat.jpg", "description" => "A very fat cat looks happy."];
- // Assign by Value
- function createMeme($meme){
- $meme ["top_text"] = "Much PHP";
- $meme ["bottom_text"] = "Very programming. Wow.";
- return $meme; //Remember to return the array
- }
- $php_doge = createMeme($doge_meme);
- print_r($php_doge); // the array value changes
- print_r($doge_meme); // this array value remains the same - does not affect its original
- // Assign by Reference - using & on the variable array
- function fixMeme(&$meme){
- $meme ["top_text"] = "Apples & Pears";
- $meme ["bottom_text"] = "Sweet fruits";
- return $meme;
- }
- $newMeme= fixMeme($bad_meme);
- print_r($newMeme);
- print_r($bad_meme); // both the arrays have their values changes and they are the same
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement