Important 50 PHP Array Programming Questions | Practice Now, TechnoVlogs

Important 50 PHP Array Programming Questions | Practice Now


Q1. Write a PHP script to create an array of integers and print each element.  
  Input: [10, 20, 30]  
  Expected Output:  
    10
    20
    30
    
Q2. Write a PHP script to find the sum of all elements in an array.  
  Input: [5, 10, 15]  
  Expected Output: Sum: 30

Q3. Write a PHP script to find the largest element in an array.  
  Input: [12, 45, 7, 22]  
  Expected Output: Largest: 45

Q4. Write a PHP script to sort an array in ascending order.  
  Input: [4, 2, 9, 1]  
  Expected Output: [1, 2, 4, 9]

Q5. Write a PHP script to reverse an array.  
  Input: [1, 2, 3, 4]  
  Expected Output: [4, 3, 2, 1]

Q6. Write a PHP script to merge two arrays.  
  Input: [1, 2] and [3, 4]  
  Expected Output: [1, 2, 3, 4]

Q7. Write a PHP script to find the difference between two arrays.  
  Input: [1, 2, 3] and [2, 3, 4]  
  Expected Output: [1]

Q8. Write a PHP script to remove duplicate values from an array.  
  Input: [1, 2, 2, 3, 4, 4]  
  Expected Output: [1, 2, 3, 4]

Q9. Write a PHP script to check if a specific value exists in an array.  
  Input: Array: [1, 5, 8], Value: 5  
  Expected Output: 5 exists in the array.

Q10. Write a PHP script to find the length of an array.  
   Input: [10, 20, 30, 40]  
   Expected Output: Length: 4

Q11. Write a PHP script to add an element to the end of an array.  
   Input: [1, 2, 3], Add: 4  
   Expected Output: [1, 2, 3, 4]

Q12. Write a PHP script to add an element to the beginning of an array.  
   Input: [2, 3, 4], Add: 1  
   Expected Output: [1, 2, 3, 4]

Q13. Write a PHP script to remove the last element from an array.  
   Input: [1, 2, 3, 4]  
   Expected Output: [1, 2, 3]

Q14. Write a PHP script to remove the first element from an array.  
   Input: [1, 2, 3, 4]  
   Expected Output: [2, 3, 4]

Q15. Write a PHP script to find the index of a specific element in an array.  
   Input: Array: [3, 8, 15], Value: 8  
   Expected Output: Index: 1

Q16. Write a PHP script to split an array into chunks of a specified size.  
   Input: [1, 2, 3, 4, 5], Chunk Size: 2  
   Expected Output: [[1, 2], [3, 4], [5]]

Q17. Write a PHP script to join all elements of an array into a string with a delimiter.  
   Input: [1, 2, 3], Delimiter: ,  
   Expected Output: 1,2,3

Q18. Write a PHP script to split a string into an array using a delimiter.  
   Input: String: 1,2,3, Delimiter: ,  
   Expected Output: [1, 2, 3]

Q19. Write a PHP script to create an associative array with keys name, age, and city. Print all key-value pairs.  
   Input: name: John, age: 30, city: NY  
   Expected Output:  
     name: John
     age: 30
     city: NY
     
Q20. Write a PHP script to find all keys of an associative array.  
   Input: ['name' => 'John', 'age' => 25]  
   Expected Output: ['name', 'age']

Q21. Write a PHP script to find all values of an associative array.  
   Input: ['name' => 'Alice', 'age' => 28]  
   Expected Output: ['Alice', 28]

Q22. Write a PHP script to sort an associative array by its keys.  
   Input: ['b' => 2, 'a' => 1]  
   Expected Output: ['a' => 1, 'b' => 2]

Q23. Write a PHP script to sort an associative array by its values.  
   Input: ['b' => 3, 'a' => 2]  
   Expected Output: ['a' => 2, 'b' => 3]

Q24. Write a PHP script to combine two arrays into an associative array.  
   Input: Keys: ['name', 'age'], Values: ['Emma', 25]  
   Expected Output: ['name' => 'Emma', 'age' => 25]

Q25. Write a PHP script to create a multidimensional array representing a 2x2 matrix.  
   Input: None  
   Expected Output:  
     [
       [1, 2],
       [3, 4]
     ]

Q26. Write a PHP script to calculate the sum of all values in a multidimensional array.  
   Input: [[1, 2], [3, 4]]  
   Expected Output: Sum: 10

Q27. Write a PHP script to flatten a multidimensional array.  
   Input: [[1, 2], [3, 4]]  
   Expected Output: [1, 2, 3, 4]

Q28. Write a PHP script to find the intersection of two arrays.  
   Input: [1, 2, 3] and [2, 3, 4]  
   Expected Output: [2, 3]

Q29. Write a PHP script to shuffle the elements in an array.  
   Input: [1, 2, 3, 4]  
   Expected Output: Randomized array.

Q30. Write a PHP script to replace specific elements in an array.  
   Input: [1, 2, 3], Replace 2 with 4  
   Expected Output: [1, 4, 3]

Q31. Write a PHP script to filter out all even numbers from an array.  
   Input: [1, 2, 3, 4, 5]  
   Expected Output: [1, 3, 5]

Q32. Write a PHP script to map an array of numbers to their squares.  
   Input: [1, 2, 3, 4]  
   Expected Output: [1, 4, 9, 16]

Q33. Write a PHP script to check if all elements in an array are positive numbers.  
   Input: [1, 2, -3, 4]  
   Expected Output: Not all elements are positive.

Q34. Write a PHP script to count the frequency of each element in an array.  
   Input: [1, 2, 2, 3, 3, 3]  
   Expected Output:   
     1: 1
     2: 2
     3: 3

Q35. Write a PHP script to find the first element in an array that is greater than 10.  
   Input: [5, 8, 12, 3]  
   Expected Output: 12

Q36. Write a PHP script to slice an array to include only the first 3 elements.  
   Input: [1, 2, 3, 4, 5]  
   Expected Output: [1, 2, 3]

Q37. Write a PHP script to remove specific elements from an array by value.  
   Input: [1, 2, 3, 4], Remove: 2  
   Expected Output: [1, 3, 4]

Q38. Write a PHP script to find the common elements between two arrays.  
   Input: [1, 2, 3] and [3, 4, 5]  
   Expected Output: [3]

Q39. Write a PHP script to find the union of two arrays.  
   Input: [1, 2] and [2, 3]  
   Expected Output: [1, 2, 3]

Q40. Write a PHP script to repeat the elements of an array 3 times.  
   Input: [1, 2, 3]  
   Expected Output: [1, 2, 3, 1, 2, 3, 1, 2, 3]

Q41. Write a PHP script to check if an array is empty.  
   Input: []  
   Expected Output: The array is empty.

Q42. Write a PHP script to extract keys from an associative array.  
   Input: ['name' => 'John', 'age' => 25]  
   Expected Output: ['name', 'age']

Q43. Write a PHP script to extract values from an associative array.  
   Input: ['name' => 'John', 'age' => 25]  
   Expected Output: ['John', 25]

Q44. Write a PHP script to add a key-value pair to an associative array.  
   Input: ['name' => 'Alice'], Add: age => 30  
   Expected Output: ['name' => 'Alice', 'age' => 30]

Q45. Write a PHP script to remove a key-value pair from an associative array.  
   Input: ['name' => 'Alice', 'age' => 30], Remove: age  
   Expected Output: ['name' => 'Alice']

Q46. Write a PHP script to check if a key exists in an associative array.  
   Input: Array: ['name' => 'Bob'], Key: name  
   Expected Output: Key 'name' exists.

Q47. Write a PHP script to group elements of an array by their length.  
   Input: ['cat', 'dog', 'lion', 'tiger']  
   Expected Output:  
     3: ['cat', 'dog']
     4: ['lion']
     5: ['tiger']

Q48. Write a PHP script to find the sum of elements at even indices in an array.  
   Input: [10, 20, 30, 40, 50]  
   Expected Output: Sum: 90

Q49. Write a PHP script to check if two arrays are equal.  
   Input: [1, 2, 3] and [1, 2, 3]  
   Expected Output: The arrays are equal.

Q50. Write a PHP script to create an array of numbers from 1 to 10.  
   Input: None  
   Expected Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Share on Social Media