- Write a Code For Sum of Two Arrarys
- Write a PHP code to merge two arrays without using built-in functions like array_merge() or array_push().
- Write a PHP code to remove duplicates from an array without using built-in functions like array_unique().
- Write a PHP code to find the common elements between two arrays without using built-in functions like array_intersect().
- Write a PHP code to calculate the sum of all elements in a multidimensional array
- Write a PHP code to generate a random array with unique elements
- Write a PHP code to rotate an array by a given number of positions.
- Write a PHP code to flatten a multidimensional array into a single-level array.
- Write a PHP code to find the longest subarray with sum equal to a given number
- Write a PHP code to find all pairs of elements in an array that sum up to a given number
- Write a PHP code to sort an array of objects by a specific property
- Write a code to find the max, min values in an array without using any inbuilt functions.
Merge two arrays without using built-in functions:
function mergeArrays($arr1, $arr2) { foreach ($arr2 as $element) { $arr1[] = $element; } return $arr1; } // Example usage: $arr1 = [1, 2, 3]; $arr2 = [4, 5, 6]; $result = mergeArrays($arr1, $arr2); print_r($result);
Remove duplicates from an array without using built-in functions:
function removeDuplicates($arr) {
$result = [];
foreach ($arr as $element) {
if (!in_array($element, $result)) {
$result[] = $element;
}
}
return $result;
}
// Example usage:
$arr = [1, 2, 2, 3, 4, 4, 5];
$result = removeDuplicates($arr);
print_r($result);
Find common elements between two arrays without using built-in functions:
function find_common_elements($arr1, $arr2) {
$common_elements = array();
foreach ($arr1 as $elem1) {
foreach ($arr2 as $elem2) {
if ($elem1 === $elem2) {
$common_elements[] = $elem1;
break;
}
}
}
return $common_elements;
}
// Example usage:
$array1 = [1, 2, 3, 4, 5];
$array2 = [3, 4, 5, 6, 7];
echo "Common elements: " . implode(', ', find_common_elements($array1, $array2));
- Calculate the sum of all elements in a multidimensional array:
- function sumMultidimensionalArray($arr) { $sum = 0; array_walk_recursive($arr, function($item) use (&$sum) { $sum += $item; }); return $sum; } // Example usage: $arr = [[1, 2], [3, [4, 5]]]; $result = sumMultidimensionalArray($arr); echo $result;
Generate a random array with unique elements:
function generateRandomArray($length, $min, $max) {
$result = [];
while (count($result) < $length) {
$random = rand($min, $max);
if (!in_array($random, $result)) {
$result[] = $random;
}
}
return $result;
}
// Example usage:
$result = generateRandomArray(5, 1, 10);
print_r($result);
Rotate an array by a given number of positions:
function rotateArray($arr, $positions) {
$length = count($arr);
$positions = $positions % $length; // normalize positions
if ($positions < 0) {
$positions += $length;
}
return array_merge(array_slice($arr, $positions), array_slice($arr, 0, $positions));
}
// Example usage:
$arr = [1, 2, 3, 4, 5];
$result = rotateArray($arr, 2);
print_r($result);
Flatten a multidimensional array into a single-level array
function flattenArray($arr) {
$result = [];
array_walk_recursive($arr, function($item) use (&$result) {
$result[] = $item;
});
return $result;
}
// Example usage:
$arr = [[1, 2], [3, [4, 5]]];
$result = flattenArray($arr);
print_r($result);
Find the max, min values in an array
<?php
// Function to find the minimum element of an array
function findMin($arr) {
$min = $arr[0]; // Initialize min with the first element of the array
$length = count($arr); // Get the length of the array
// Loop through the array starting from the second element
for ($i = 1; $i < $length; $i++) {
// If the current element is smaller than min, update min
if ($arr[$i] < $min) {
$min = $arr[$i];
}
}
return $min; // Return the minimum element
}
// Function to find the maximum element of an array
function findMax($arr) {
$max = $arr[0]; // Initialize max with the first element of the array
$length = count($arr); // Get the length of the array
// Loop through the array starting from the second element
for ($i = 1; $i < $length; $i++) {
// If the current element is greater than max, update max
if ($arr[$i] > $max) {
$max = $arr[$i];
}
}
return $max; // Return the maximum element
}
// Sample array
$array = array(3, 1, 6, 9, 2, 8);
// Find minimum and maximum elements
$min = findMin($array);
$max = findMax($array);
// Output the results
echo "Minimum element: $min<br>";
echo "Maximum element: $max<br>";
?>
0 comments:
Post a Comment