array function in php
snippet in php

common array methods php

user7647

sizeof($arr) //returns the size of elements in the array
is_array($arr) // returns true if $arr is an array and false otherwise
in_array($var, $arr) // check if $var is in the array $arr
print_r($arr) // prints the complete representation of the array
array_merge($arr1, $arr2) //combines $arr1 and $arr2 into a single array
array_values($arr) //store all the values into a new array without the keys but only the values
array_keys($arr) // returns only the keys inside an array
array_pop($arr) // removes the last element of  the array
array_push($arr, $val) // pushes $val to the end of array
array_shift($arr) // removes the first element in the array $arr
sort($arr) // sorts an array in an ascending order

/*Other sorting methods are:
-asort()
-arsort()
-ksort()
-krsort()
-rsort()
*/

array_map('function_name', $arr) // passes each value in the array inside the fuction and do the operation on the array data
array_flip($arr) //This function interchange the keys and the values of a PHP associative array.
array_reverse($arr) //This function is used to reverse the order of elements
array_rand($arr) //randomly pick an element from the array
array_slice($arr, $offset, $length)//This function is used to create a subset of any array





php delete element by value

user7895

$colors = array("blue","green","red");

//delete element in array by value "green"
if (($key = array_search("green", $colors)) !== false) {
    unset($colors[$key]);
}

php if array has mroe than one subarray

user7885

if (count($arr) > 1) {
     ....
}

how to remove keys in subarray php

user5453

print_r(deep_delete_keys($arr,'country'));

function deep_delete_keys($arr, $keys) {
    if (!is_array($keys)) $keys = array($keys);
    $filteredArr = array_diff_key( $arr, array_flip( $keys ) );
    foreach ($filteredArr as &$val) {
        if (is_array($val)) {
            $val = deep_delete_keys($val, $keys);
        }
    }
    return $filteredArr;
}

array function in php

user4931

array_change_key_case()	Changes all keys in an array to lowercase or uppercase
array_chunk()	Splits an array into chunks of arrays
array_column()	Returns the values from a single column in the input array
array_combine()	Creates an array by using the elements from one "keys" array and one "values" array
array_count_values()	Counts all the values of an array
array_diff()	Compare arrays, and returns the differences (compare values only)
array_diff_assoc()	Compare arrays, and returns the differences (compare keys and values)
array_diff_key()	Compare arrays, and returns the differences (compare keys only)
array_diff_uassoc()	Compare arrays, and returns the differences (compare keys and values, using a user-defined key comparison function)
array_diff_ukey()	Compare arrays, and returns the differences (compare keys only, using a user-defined key comparison function)
array_fill()	Fills an array with values
array_fill_keys()	Fills an array with values, specifying keys
array_filter()	Filters the values of an array using a callback function
array_flip()	Flips/Exchanges all keys with their associated values in an array
array_intersect()	Compare arrays, and returns the matches (compare values only)
array_intersect_key()	Compare arrays, and returns the matches (compare keys only)
array_key_exists()	Checks if the specified key exists in the array
array_keys()	Returns all the keys of an array
array_map()	Sends each value of an array to a user-made function, which returns new values
array_merge()	Merges one or more arrays into one array
array_merge_recursive()	Merges one or more arrays into one array recursively
array_multisort()	Sorts multiple or multi-dimensional arrays
array_pad()	Inserts a specified number of items, with a specified value, to an array
array_pop()	Deletes the last element of an array
array_product()	Calculates the product of the values in an array
array_push()	Inserts one or more elements to the end of an array
array_rand()	Returns one or more random keys from an array
array_reduce()	Returns an array as a string, using a user-defined function
array_replace()	Replaces the values of the first array with the values from following arrays
array_replace_recursive()	Replaces the values of the first array with the values from following arrays recursively
array_reverse()	Returns an array in the reverse order
array_search()	Searches an array for a given value and returns the key
array_shift()	Removes the first element from an array, and returns the value of the removed element
array_slice()	Returns selected parts of an array
array_splice()	Removes and replaces specified elements of an array
array_sum()	Returns the sum of the values in an array
array_udiff()	Compare arrays, and returns the differences (compare values only, using a user-defined key comparison function)
array_udiff_assoc()/ array_udiff_uassoc()	Compare arrays, and returns the differences (compare keys and values, using a built-in function to compare the keys and a user-defined function to compare the values)
array_uintersect()	Compare arrays, and returns the matches (compare values only, using a user-defined key comparison function)
array_uintersect_assoc()/array_uintersect_uassoc()	Compare arrays, and returns the matches (compare keys and values, using a built-in function to compare the keys and a user-defined function to compare the values)
array_unique()	Removes duplicate values from an array
array_unshift()	Adds one or more elements to the beginning of an array
array_values()	Returns all the values of an array
array_walk()	Applies a user function to every member of an array
array_walk_recursive()	Applies a user function recursively to every member of an array
arsort()	Sorts an associative array in descending order, according to the value
asort()	Sorts an associative array in ascending order, according to the value
compact()	Create array containing variables and their values
count()	Returns the number of elements in an array
current()	Returns the current element in an array
end()	Sets the internal pointer of an array to its last element
extract()	Imports variables into the current symbol table from an array
in_array()	Checks if a specified value exists in an array
key()	Fetches a key from an array
krsort()	Sorts an associative array in descending order, according to the key
ksort()	Sorts an associative array in ascending order, according to the key
next()	Advance the internal array pointer of an array
rsort()	Sorts an indexed array in descending order
shuffle()	Shuffles an array
sizeof()	Alias of count()
sort()	Sorts an indexed array in ascending order
uasort()	Sorts an array by values using a user-defined comparison function
uksort()	Sorts an array by keys using a user-defined comparison function
usort()	Sorts an array using a user-defined comparison function

create a function that checks the values of the indexes in two arrays and keep a score

user1600


const triplets = (arr1,arr2) => {
  let score1 = 0;
  let score2 = 0;
  let resultArr = [0,0]
  for (let i = 0; i < arr1.length; i++){
    if(arr1[i] === arr2[i]) {
      resultArr[0] = score1
      resultArr[1] = score2
    } else if (arr1[i] > arr2[i]) {
      score1++
      resultArr[0] = score1
    } else if (arr1[i] < arr2[i]) {
      score2++
      resultArr[1] = score2
    }
  }
  return resultArr
}