Remove Null Values from a PHP Array: 5 Code Examples (2023)

How to Remove Null Values from an Array in PHP Use the unset() & is_null() functions to remove null values from a PHP array. You can also use the type check operator "===" as an alternative to the is_null function. You can check for false or use the empty() function if you need to remove any empty value, including nulls. Removing Null Values from PHP Arrays Code Example <?php $array = array('hello', null, 'world', null); foreach ($array as $key => $value) { if ($value === null) { unset($array[$key]); } } print_r(array_Values($array)); // Output: Array ( [0] => hello [1] => world ) ?> Functions to Check for null & empty values in PHP Function/MethodDescriptionisset()Determines if a variable is set and is not null. Returns true if the variable exists and has a value other than null, and false otherwise.empty()Determines if a variable is considered empty. Returns true if the variable…

read more

PHP in_array(): Check if Array Contains Value (5 Code Examples)

How to Use PHP in_array function to check if the array contains a value Arrays are an important data structure in PHP, allowing developers to store and manipulate a collection of values. Often, developers need to check if an array contains a specific value. One way to accomplish this in PHP is using the in_array() function. This function returns a boolean value indicating whether a specified value exists in an array.  This article will discuss the syntax and usage of the in_array() function with examples. PHP in_array Code Example <?php /* Recursive approach */ $students = array( array("name" => "John", "age" => 20, "courses" => array("Math", "Science")), array("name" => "Mary", "age" => 22, "courses" => array("History", "English")), array("name" => "Peter", "age" => 24, "courses" => array("Computer Science", "Physics")) ); function searchArray($needle, $haystack) { foreach($haystack as $value) { //If array within array if(is_array($value)) { //Recursively call the function on the array.…

read more

PHP array_filter: 15 Array Filter Code Examples + Load Tests

How to use the PHP array_filter function to filter PHP Arrays Code Example <?php $dogBreedsILove = ["chihuaha", "collie", "golden retriever", "terrier"]; // I need all of the retreivers in the list. $retrievers = array_filter($dogBreedsILove, function ($breed) { return stripos($breed, "retriever"); } print_r($dagBreedsILove); PHP Array Filter Learning Path This article is part of our large series on filtering arrays. Feel free to browse through the articles we have listed below and dig further into some unique scenarios. These articles will help you level up your PHP development skills How to filter PHP associative array How to use Doctrine ArrayCollection map and filter functions Array of objects in PHP | How to create, sort, filter, merge & search them Filter an array of objects by values in PHP How to filter arrays of objects by keys in PHP Filter multidimensional array by value in PHP PHP Arrays Array Filter Load Testing: array_filter…

read more

array_map, array_walk, & array_filter: What’s the Difference (2023)

array_map array_walk array_filter
This article reviews the array_map, array_walk, and array_filter functions and includes examples to clarify their differences. If you’re already familiar with these functions, feel free to skip to the last section for a quick overview.  Using the PHP array_map vs array_walk & array_filter functions PHP array_map, array_walk, and array_filter are higher-order functions. These functions take functions as arguments, commonly known as callback functions. If you’re coming from a Javascript background, you can understand the significance of the callbacks function in asynchronous programming. However, PHP uses these callback functions on an array and consequently accesses, transforms, or filters array elements based on the logic of the callback function. Some widely known higher order functions are array_map, array_walk, and array_filter. FuelingPHP has in-depth articles on each of these.  This article aims to analyze these functions and emphasize their differences comparatively. The article includes sections on each of these with relevant examples to…

read more

Using array_keys vs array_values: 5 PHP Code Examples (2023)

difference between array_keys and array_values
PHP array_keys and PHP array_values are two important functions. The array_keys function returns the keys from the input array, and the array_values return the values. The array_keys take some additional parameters for returning keys corresponding to a value that matches the search_value argument. Both of these functions return an array. So, this article reviews these functions and helps the readers understand the difference between array_keys and array_values. To have heads on, here are the articles explaining the PHP arrays_keys and PHP array_values in depth. PHP array_keys: how to use in PHP with examples PHP array_values: how to use in PHP with examples What is the difference between array_keys and array_values This section reviews the array_keys PHP and the array_values PHP and includes examples to clarify the difference between array_keys and array_values. what is the array_keys function Here’s an overview of the array_keys in PHP. Function Signature array_keys(array $array, mixed $search_value,…

read more

array_merge vs array_merge_recursive: PHP Code Examples

Difference between array_merge and array_merge_recursive
We have already seen the functions array_merge and array_merge_recursive. As their names suggest, they help merge arrays in PHP. However, it is necessary to understand the core differences in these functions. This understanding will help you use the correct function in the correct place. In this article, we will explore the difference between array_merge and array_merge_recursive in PHP. Before that, we will recommend you to get yourself familiarised with these functions. Here are the links to the articles we have done for you. PHP array_merge : How to use with examples in PHP PHP array_merge_recursive: How to use with examples in PHP Once you’re done, we are good to go. What is the difference between array_merge and array_merge_recursive? As we have mentioned already, these functions merge PHP arrays. Then what’s the difference? The difference lies in their dealing with similar string keys. PHP array_merge and array_merge_recursive distinctively behave when they…

read more

array_walk vs array_walk_recursive: Whats the Difference (PHP Code Examples)

array_walk and array_walk_recursive
Introduction We have seen array_walk and array_walk_recursive functions already. In this article, we will know the difference between array_walk and array_walk_recursive. To get the most of this article, we recommend understanding these functions really well. The fundamental difference between these functions is subtle, and that is, array_walk doesn’t dig deep into multidimensional arrays. Contrary to this, array_walk_recursive uses recursion to access the values in the innermost arrays of a multidimensional array. Alternatively, we can say that it accesses the values that are leaf nodes of the resulting tree structure. Before we jump to the main article, let’s first review the array we will be using in the examples and its resulting tree structure. Developers Array $developers = [ "PHP" => [ "Nina" => ["Experience(Years)"=>5,"Skills"=>["PHP","Laravel","Git"]], "Frederick" => ["Experience(Years)"=>4,"Skills"=>["PHP","Laravel","MVC","Git"]], ], "Javascript" => [ "Allen" => ["Experience(Years)"=>5,"Skills"=>["React","React Native","Git"]], "Monty" => ["Experience(Years)"=>4,"Skills"=>["Nodejs","Express","Nestjs","Git"]], ], "Java" => [ "Franklin" => ["Experience(Years)"=>5,"Skills"=>["Java Spring","MVC","Git"]], "Daniel" => ["Experience(Years)"=>4,"Skills"=>["Android","Adobe XD","Git"]],…

read more