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:

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?
PHP: Tips of the Day
PHP: Anonymous recursive PHP functions
In order for it to work, you need to pass $factorial as a reference
$factorial = function( $n ) use ( &$factorial ) { if( $n == 1 ) return 1; return $factorial( $n - 1 ) * $n; }; print $factorial( 5 );
Ref : https://bit.ly/38dj7jm
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework