w3resource

PHP Array Exercises : Create a range of specified arrays

PHP Array: Exercise-59 with Solution

Write a PHP program to create a range like the following array.

Array
(
[20] => 2
[21] => 3
[22] => 4
[23] => 5
[24] => 6
[25] => 7
)

Solution:

PHP Code:

<?php
// Use the array_combine function to create an associative array
// The keys are generated using the range function for values from 20 to 25
// The values are generated using the range function for values from 2 to 7
$result = array_combine(range(20, 25), range(2, 7));

// Print the result using the print_r function
print_r($result);

?>

Sample Output:

Array                                                       
(                                                           
    [20] => 2                                               
    [21] => 3                                               
    [22] => 4                                               
    [23] => 5                                               
    [24] => 6                                               
    [25] => 7                                               
) 

Flowchart:

Flowchart: PHP - Create a range of specified arrays

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a PHP script to combine (using one array for keys and another for its values) the specified two arrays.
Next: PHP For Loop Exercises Home.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/php-exercises/php-array-exercise-59.php