Ad
Saturday, December 9, 2023
Chapter 3 Arrays
-Which construct is used to define an array?
The array() construct is used to define an array in PHP.
-How to convert an object to array?
The get_object_vars() function to convert an object to an associative array.
-What is associative array? Explain with example how is it different from indexed
array
An associative array in PHP is an array that uses named keys instead of numerical indices to store data. Each key is associated with a specific value. Example:
$person = array(
"name" => "John",
"age" => 30,
"city" => "New York"
);
Therefore, indexed arrays where data is accessed by numerical indices ($array[0]), associative arrays allow access using descriptive keys ($person['name']).
-How will you find number of elements in array?
the count function to find the number of elements in an array.
$array = [1, 2, 3, 4, 5];
$numElements = count($array);
echo "Number of elements: " . $numElements;
- Which function is used to remove duplicate elements from
an array ?
The array_unique function is used to remove duplicate elements from an array.
-How to get array of keys and array of values from an associative
array ? Illustrate with suitable example using built-in functions.
$associativeArray = array("name" => "John", "age" => 25, "city" => "New York");
// Get array of keys
$keys = array_keys($associativeArray);
// Get array of values
$values = array_values($associativeArray);
print_r($keys); // Output: Array ( [0] => name [1] => age [2] => city )
print_r($values); // Output: Array ( [0] => John [1] => 25 [2] => New York )
- Explain the following function :
(1) array-flip( )
(2) array-filter( )
(3) is-object( )
(4) die( )
array_flip():
Purpose: Swaps the keys and values of an array.
Example:
$originalArray = array("a" => 1, "b" => 2, "c" => 3);
$flippedArray = array_flip($originalArray);
print_r($flippedArray);
// Output: Array ( [1] => a [2] => b [3] => c )
array_filter():
Purpose: Filters elements of an array using a callback function.
Example:
$numbers = array(1, 2, 3, 4, 5);
$filteredNumbers = array_filter($numbers, function($value) {
return $value % 2 == 0; // Keep only even numbers
});
print_r($filteredNumbers);
// Output: Array ( [1] => 2 [3] => 4 )
is_object():
Purpose: Checks if a variable is an object.
Example:
$obj = new stdClass();
$result = is_object($obj);
echo $result ? 'It is an object' : 'It is not an object';
// Output: It is an object
die() (or exit()):
Purpose: Terminates the script and outputs a message.
Example:
$age = 17;
if ($age < 18) {
die('You must be 18 or older.');
}
// Script won't continue if the condition is true.
-How to Insert and Remove the last element of an array
-Explain the following functions with respect to Array
1) Extract()
2) Shuffle()
3) Array_splice()
4) Arsort()
-How to check if given variable is Array or not?
-What is array ? Write a short note on multidimensional array with examples.
- State the purpose of Array_filter ( )function.
- Explain the following functions with respect to Array :
Explode( )
Array_unshift( )
Array_slice( )
Krsort( )
- Define array_unique( ).
-What is an associative array? Give an example.
About Sanket
Qna Library Is a Free Online Library of questions and answers where we want to provide all the solutions to problems that students are facing in their studies. Right now we are serving students from maharashtra state board by providing notes or exercise solutions for various academic subjects
No comments:
Post a Comment