https://github.com/php/php-src
I will learn the array methods part 2 (Array_Search, In_Array, Array_Key_Exists, Array_Unshift, Array_Push, Array_Shift and Array_Pop).
In this tutorial we will continue our series about PHP programming language, we will take 3 concepts , how to search in array , how to add values to an array and how to remove from an array using the array methods ( array_search, in array, array_key_exists , array_unshift , array_push,array_ pop and array_shift ), let's start ..
There are many ways to search a value in a particular array, in this tutorial we will use the " Array_Search and In_Array " for the indexed arrays and " Array_Key_Exists " for the associative arrays.
A - Array_Search Method :
The array_search () method searches for a specified value and returns the key to the first element that holds this value if it succeeds. This method in the "haystack" array looks for the value of the "needle".
The needle Is the value that we search and the Haystack Is the array that we will search in.
If the needle is a string, the comparison will be sensitive to the letters case, this method returns the element key to the value of the needle if it is found in the array, or returns FALSE if it's not found.
To use the array_search() method we must pass two parameters at least, the needle and the haystack
array_search(Needle, Array);
In my example I have created an indexed array and I will search if " Alex " exists in my array and this is my code
if(array_search("Alex", $friends)){
echo "The element exists! (Array_Search)";
}
The array_search will return true with the key if the element exists , so if the condition is true I will print by the echo method "The element exists!" and this is the result
B - In_Array Method :
The in_array () method checks for a value in an array, the same as array search it will return the key that holds the value if it succeeds or false if it fails.
Always we have 3 parameters , haystack, needle and the strict value " True or False ".
The Strict Parameter:
If we make the third parameter true, the method will check if the needle and the value found in the haystack are equals , if they are equals it will return the key , else it will return false.
To use the in_array method we pass three parameters " the needle, the haystack and the strict "
in_array(needle, haystack, strict);
I will continue with the previous example, I will check if " alex " exists in the friends array by the ' in_array ' method
echo "<h3>In_Array</h3>";
if(in_array("alex", $friends,true)){
echo "The element exists! (In_Array)";
} else {
echo "The element <span style='color:red;font-size:20px'>not</span> exists! (In_Array)";
}
The value that I passed is " alex " with small " a " and this is the result, it will execute the else content
C - Array_Key_Exists Method :
This method verifies that the key or index exists within the array. The method returns the value true if the key is present . And false if the key is not present in the array .
The array_key_exists will look for keys within the first dimension only. The method will not find nested keys in multidimensional arrays.
To use the array_key_exists method we pass the array and the key
array_key_exists(key, array);
I will use the " languages " array because it's an associative array , I will search for the key " en " and this is my code
echo "<h3>Array_Key_Exists</h3>";
if(array_key_exists("en", $languages)) {
echo "The element exists! (Array_Key_Exists)";
}
If the "en" key exists in our languages array the system will print " The element exists! " and this is the result
You can add more values to your array and there are many methods to add , in this tutorial we will use " Unshift and Push methods".
A - Array Unshift Method :
This method adds one or more elements at the beggining of the array.
Note that the list of items enters the beginning of the array at once, so that the inserted elements remain in the same order. All the numeric array keys will be modified to start counting from scratch while the craft keys will not change.
To use the array_unshift we need to pass the array and the values
array_unshift(array, values);
I will use the friends array to add a new friend
echo "<h3>Unshift Method </h3>";
array_unshift($friends, "Mick");
echo "<h3>My Friends </h3>";
foreach ($friends as $friend) {
echo $friend . " is my friend <br>";
}
I have added a new friend named " Mick " and I will print the new array
B - Array Push Method :
This method adds one or more elements at the end of the array.
The array_push () method treats the array as a stack, drives the variables we passed to the end of the array, and increases its length by the number of variables we add.
To use the array_unshift we need to pass the array and the values
array_push(array, values);
I will add a new friend at the end of our friends using the array_push method
echo "<h3>Push Method </h3>";
array_push($friends, "Arden");
echo "<h3>My Friends </h3>";
foreach ($friends as $friend) {
echo $friend . " is my friend <br>";
}
I will add " Arden " at the end of the array , we can use this method to add values to the associative arrays also
You can remove elements from an array using the remove methods , we will learn the " array_shift and array_pop " methods.
A - Array Shift Method :
Array_shift () removes the first element of the array and returns its value, shortening the array by one element and displacing all elements towards the beginning of the array.
Modifies all the numerical keys of the array to start counting from scratch while the literal keys do not change.
To use this method we need to pass only the array as parameter
array_shift(array);
We have added " Mick " using the " Unshift " method and now we will remove " Mick " using the " Shift" Method
echo "<h3>Shift Method </h3>";
array_shift($friends);
echo "<h3>My Friends </h3>";
foreach ($friends as $friend) {
echo $friend . " is my friend <br>";
}
We will remove the first element of the " friends " array , " Mick " will be removed and this is the result
B - Array Pop Method :
The array_pop () method drives the last element of the array and returns its value, reducing the length of the array by one element. The method returns the value of the last element of the array.
If the array is empty ,the function returns the value " NULL ", it will give an E_WARNING error when used on a non-array value.
To use this method we need to pass only the array as parameter
array_pop(array);
We have added " Arden " using the array_push method and now we will remove it using the array_pop method
echo "<h3>Pop Method </h3>";
array_pop($friends);
echo "<h3>My Friends </h3>";
foreach ($friends as $friend) {
echo $friend . " is my friend <br>";
}
Arden is the last element of the array and the pop method will remove the last element, this is the result
https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/search_add_remove.php