Lesson 1
Using Nested Loops and Arrays in PHP for Finding Integer Pairs
Introduction

Welcome to our programming practice lesson! Are you ready for a challenging yet exciting task involving nested loops and arrays? We will unravel the skill of using nested loops to search through two arrays. Brace yourself for a remarkable journey of practical learning. Let's get started!

Task Statement

Imagine a scenario where you are given two lists of integers. Your task is to write a function that retrieves and returns pairs of integers. The first item of the pair will be from the first list, while the second one will come from the second list. It's crucial to remember that the first element must be less than the second.

The sequence of pairs in your output should align with the order they appear in the input lists. For instance, given the lists [1, 3, 7] and [2, 8, 9], the function should return ["1 2", "1 8", "1 9", "3 8", "3 9", "7 8", "7 9"]. It will pose a challenge if no pairs exist or if any input list is empty. Let's delve into this task step by step to uncover the solution!

Building the Solution: Step 1

Before venturing into the code, let's decode the problem. Nested looping fits perfectly here.

Start by creating an empty array named $result to store our pairs.

php
1<?php 2 3function retrievePairs($list1, $list2) { 4 $result = [];

Creating your function and data structure first is a wise strategy!

Building the Solution: Step 2

Now, the focus turns to forming the nested loops. You need to iterate over both lists, and for this, you'll need nested loops. An outer loop will select one element from the first list, and an inner loop will scan through each element of the second list.

php
1<?php 2 3function retrievePairs($list1, $list2) { 4 $result = []; 5 foreach ($list1 as $i) { 6 foreach ($list2 as $j) { 7 // Our logic goes here 8 } 9 } 10 return $result; 11}

In this setup, every element in $list1 is represented by $i, and for each $i, $j represents an element in $list2.

Building the Solution: Step 3

With our loops ready, it's time to incorporate the logic. We run a check at this point: Is the element $i from $list1 less than the element $j from $list2? If true, we insert the concatenated string "$i $j" into our $result array.

php
1<?php 2 3function retrievePairs($list1, $list2) { 4 $result = []; 5 6 // Start of the outer loop: iterating through elements of list1 7 foreach ($list1 as $i) { 8 // Start of the inner loop: iterating through elements of list2 for each element of list1 9 foreach ($list2 as $j) { 10 // Check if the element from list1 is less than the element from list2 11 if ($i < $j) { 12 // Add the valid pair to the result array 13 $result[] = "$i $j"; 14 } 15 } 16 } 17 // Return the final array of pairs 18 return $result; 19} 20 21$list1 = [1, 3, 7]; 22$list2 = [2, 8, 9]; 23$res = retrievePairs($list1, $list2); 24 25// Print each pair from the result array 26foreach ($res as $pair) { 27 echo "$pair\n"; 28} 29// Outputs: 301 2 311 8 321 9 333 8 343 9 357 8 367 9

During each execution of our inner loop, we perform this check and store the pairs that comply with our condition.

Lesson Summary

Fantastic job! You have successfully performed a complex task using nested loops to search through two lists in PHP. You now possess the ability to traverse and manipulate two lists effectively for a given purpose. Keep practicing and continue challenging yourself with more tasks to solidify your understanding. In your upcoming practice sessions, you will encounter similar tasks that will further sharpen your programming skills. Remember, practice is the key to mastering any concept. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.