Answers for "array php functions"

PHP
17

common array methods php

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

//delete element in array by value "green"
if (($key = array_search("green", $colors)) !== false) {
    unset($colors[$key]);
}
Posted by: Guest on October-30-2019
5

common array methods php

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
Posted by: Guest on July-15-2020
0

array values php function

$fruits = ["m"=>"banana", "c"=>"Apple" , "a"=>"gray"];
$newArray = array_values($colors); //new Array with values $fruits array
echo "<pre>";
print_r( $newArray );



//output
Array
(
    [0] => banana
    [1] => gray
    [2] => blue
    [3] => num
)
Posted by: Guest on June-23-2021
1

php array functions

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_assoc()	Compare arrays and returns the matches (compare keys and values)
array_intersect_key()	Compare arrays, and returns the matches (compare keys only)
array_intersect_uassoc()	Compare arrays, and returns the matches (compare keys and values, using a user-defined key comparison function)
array_intersect_ukey()	Compare arrays, and returns the matches (compare keys only, using a user-defined key comparison function)
Posted by: Guest on October-05-2020

Browse Popular Code Answers by Language