4 life savings php array function for web developers

4 life savings php array function for web developers

Array is one of the most vital portion of every programming language. In our web scripting like php, javascripts – we can not think multiple data manipulation without array. Most of the programmer afraid(!) with array. I don’t know why?? I think array is one of the most easiest matter in this world. Today I am going to present 4 important array manipulated PHP scripts.

Today I’m going to discuss about-

1. array_change_key_case function.

2. array_chunk function.

3. array_combine function.

4. array_count_values function.

With this scripts i show you codes and corresponding demos. I hope you enjoy my tutorials. so,lets start.

1. How to use array_change_key_case() function:

* This functions takes 2 paramerts as input.

* First parameter contains an array and second parameter contains a keyword.

* There are two keywords exists for this function. such as- CASE_UPPER and CASE_LOWER.

* This function convert array index in to lowercase/uppercase depending on keywords.

– Write a live Example of array_change_key_case() function:

$input = array (
    "first" =>1,
    "second" =>2,
    "third" =>3,
    "fourth" =>4
);
print_r(array_change_key_case($input,CASE_UPPER));

Demo & Source Code

2. How to use array_chunk() function:

* This function takes an array as input and it has additional 2 parameters.

* First parameters is an array, second parameter is size fo chunk and third parameter is a Boolean value which is optional.

* If third parameter is true then its chucked array by maintain its indexed value. Otherwise chucked array index always starts from zero(0).

– Write a live Example of array_chunk() function:

$input_array = array ('a','b','c','d','e');
print_r(array_chunk($input_array,2));
print_r(array_chunk($input_array,2,true));

Demo & Source Code

3. How to use array_combine() function:

* This function takes 2 arrays as input.

* First array element considered as key and second array element considered as value.

*It’s return an array.

* It shows an error if any element is empty or number of elements does not matched.

– Write a live Example of array_combine() function:

$a = array("red","green","yellow");
$b = array("mango","apple","orange");
$c = array_combine($a,$b);
print_r($c);

Demo & Source Code

4. How to array_count_values() function:

* This function takes an array as input.

* It show the number of occurrence of a value in an array.

* It return an array.

– Write a live Example of array_count_values() function:

$input_array = array (1,"hello",1,"world","hello");
$ouput_array = array_count_values($input_array);
print_r($ouput_array);

Demo & Source Code

Bonus 2 More Array Function:

– How to get size of an array?

We can easily count the size of array element by php. There have 2 awesome function in php for counting size of array. First one is sizeof() and another is count(). Both return integer value.

5. How to use sizeof() function:

* This function takes an array as input.

* The absolute value of number. If the argument number is of type float, the return type is also float, otherwise it is integer (as float usually has a bigger value range than integer).

* It return integer value.

– Write a live Example of sizeof() function:

$input_array = array ("dog","cat","bird","man","cow");
$array_size = sizeof($input_array);            
echo "Array size:".$array_size;

Demo & Source Code

6. How to use count() function:

* This function takes an array as input.

* the number of elements in var. If var is not an array or an object with implemented Countable interface, 1 will be returned. There is one exception, if var is null, 0 will be returned.

*count may return 0 for a variable that isn’t set, but it may also return 0 for a variable that has been initialized with an empty array. Use isset to test if a variable is set.

* It return integer value.

– Write a live Example of count() function:

$input_array = array ("pink","red","yellow","blue","green");
$array_size = count($input_array);            
echo "Array size:".$array_size;

Demo & Source Code

That’s all for today.

Was this information useful? What other tips would you like to read about in the future? Share your comments, feedback and experiences with us by commenting below!

Share It

Related Posts