w3resource
php Programming Exercies

PHP Array Exercises : Print two values from a given array

PHP Array: Exercise-19 with Solution

Write a PHP script to print "second" and Red from the following array.
Sample Data :
$color = array ( "color" => array ( "a" => "Red", "b" => "Green", "c" => "White"),
"numbers" => array ( 1, 2, 3, 4, 5, 6 ),
"holes" => array ( "First", 5 => "Second", "Third"));

Sample Solution:

PHP Code:

<?php
$color = array ( "color" => array ( "a" => "Red", "b" => "Green", "c" => "White"),
"numbers" => array ( 1, 2, 3, 4, 5, 6 ),
"holes" => array ( "First", 5 => "Second", "Third"));
echo $color["holes"][5]."\n"; // prints "second"
echo $color["color"]["a"]."\n"; // prints "Red"
?>

Sample Output:

Second                                                      
Red    

Flowchart:

Flowchart: Print two values from a given array

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a PHP function to floor decimal numbers with precision.
Next: Write a PHP function to sort an array according to another array acting as a priority list.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming