Using in_array vs array_search: 3 PHP Code Examples (2023)

in_array and arraysearch
PHP in_array and array_search are two functions that seem to have a common function, that is, to search an array for a value. However, they do this function in different ways, and they have different return types. We have already seen the array_search in-depth. We recommend you to go through that article to have some insights about the function. In a nutshell, the array_search looks for a value in a PHP array and returns the corresponding key. In comparison, the in_array function looks for a value and returns true if it exists. This article compares the two functions and highlights the difference between in_array and array_search. What is the difference between in_array and array_search? This section gives a review of the two functions and elaborates the differences between in_array and array_search. After the reviews and examples, you would clearly understand the difference between these PHP functions and be more aware…

read more

Filter Array of Objects by Key: 5 PHP Code Examples (2023)

filter array of objects by key in PHP
Filter Array of Objects PHP Code Example The following code snippet is a quick way to filter array of objects by key using the array_filter function. We recommend using the method for most array filtering use cases. Feel free to use this snippet and then follow along for more explanation. <?php // Dummy data just to populate function. // I'm creating a json string as its an efficient way to create an array of objects. // You can ignore this part and replace $objectsArray with your array. $content = '[{"type": "cat", "name": "charles"}, {"type": "dog", "name": "sam"}}, {"type": "donkey": "name": "frank"}]'; $objectsArray = json_decode($content); //Array_filter to filter objects with cat as type // This is where the work is done. $filtered_arr = array_filter( $objectsArray, function($key){ return $key === 0; }, ARRAY_FILTER_USE_KEY); //Gets the filtered array. print_r($filtered_arr); /* OUTPUT Array ( [0] => stdClass ( [name] => charles [type] => cat…

read more

Join & Append PHP Strings: Concatenation Code Examples (2023)

concatenate strings in PHP
How to Concat Strings in PHP to Combine Multiple Strings Together I use several methods for concatenating strings in PHP. The dot operator is the standard method for combining multiple strings together. Sometimes, I also need PHP concat arrays. I use the implode function in these cases. Check out the examples below to learn more about joining & combining PHP strings together. Concat String Code Example: Combine & Append 2 or More Strings to Form a New String // Here's a few quick ways to append to strings in php. // let's setup the base string. $myString = "Start with one string"; // Let's do a quick concatenation example. $myString = $myString . " and add 2nd string. "; // Let's show another way to append and concatenate a string in PHP $myString .= " Here's a 3rd string added. "; // What if we have another variable string that…

read more

Array Length in PHP: Use the Count Function with Code Examples

array length in PHP
How to Get the Length of an Array in PHP PHP provides two identical functions whenever you need to get the length of an array. The count function is the most common method. You can also use the sizeOf function as it is just an alias of the count. I prefer the count function and would recommend avoiding the sizeOf function. PHP Count Function Code Example <?php //An array with many elements. $arr = array("PHP","Javascript","Python","Java","C#","C++"); //Let's count this array using the count function and print it to console. echo "Length of \$arr is: ".count($arr); //OUTPUT //Length of $arr is: 6 ?> Counting Arrays in PHP An array is a data structure to store multiple elements. Like any programming language, PHP provides helpful functions to work with arrays. We have seen many array functions in previous articles.  One common array operation is to determine its length, which is the number of…

read more

Page 7 of 7
1 5 6 7