Random Fun with Arrays3:36 with Alena Holligan
Using a built in PHP function, we’ll choose four random items to suggest on the home page.
When using array_rand, the resulting array of keys is NOT shuffled, the books will always come before movies, which will come before music. If you want to randomize these even further you could use the shuffle function as well.
$random =array_rand($catalog,4); shuffle($random);
In our last video we created our own function called get item HTML. 0:00 This allows us to get HTML for a specific item for our list. 0:05 Currently our home page loops through each of the items in the catalog. 0:09 Just like the catalog page. 0:13 Instead of the entire catalog we want to pull four 0:15 random products to suggest to our user. 0:18 To do that, we're gonna use a built-in PHP function. 0:20 PHP offers an number of built-in functions. 0:25 The PHP Docs are a great place to look for things that you want to do, and 0:27 it gives you the exact syntax that you need to use to make these functions work. 0:32 Let's check it out. 0:36 As you can see there are quite a few functions available, and 0:38 I'd encourage you to play around with them yourself. 0:41 We'll use a couple more of these built in functions later on. 0:44 For now, we're looking for a function called array_rand. 0:47 Clicking on that function gives you the details of that function and 0:54 how to use it. 0:56 Just like the function we created, 0:58 the array_rand function takes two parameters and returns a value. 1:00 The first parameter is the array and 1:04 the second parameter is the number of elements we want to grab. 1:07 Looking at return values, we see that if only one item is returned, 1:11 it returns that single key element. 1:17 If multiple items are returned, it puts the key into an array. 1:20 Let's try this out in our own code. 1:24 Let's go into index.php. 1:25 We're going to create a new. 1:28 Random variable. 1:32 We'll assign this the return 1:37 value of array_rand. 1:42 We pass in our catalog and the number four is the number of items we want to return. 1:46 Let's do a var_dump to check out these results. 1:55 The var_dump command will display all the possible information about 2:08 a particular variable on the screen. 2:11 You can see in the browser that the new random variable is an array, 2:14 with four elements, that contain the random keys from 2:19 our catalog array, 102, 103, 104 and 303. 2:24 To use this we're going to use our for each loop again but 2:29 this time we're going to just grab each key from our random array. 2:32 Let's go back into index.php, let's remove our var_dump. 2:36 And instead of catalog we want to just do random as id. 2:42 Then in our get_item_html, we'll pass the id, but instead of item, 2:50 we're gonna use our catalogue, and we'll pass in our id. 2:56 Now, when we save this page and refresh it in our browser, 3:04 we'll see that four items are selected. 3:10 When we refresh again, we see another random four items. 3:14 Great job, you've used the PHP docs to learn about and 3:20 use built-in functions that make our job as programmers a lot easier. 3:23 In the next video, we'll discuss how to pull specific items from our catalog. 3:28 In this case the category we specify. 3:33
You need to sign up for Treehouse in order to download course files.Sign up