Blog
- Details
- Written by R. Elizondo
- Category: Basics of Software Development
array_filter(callable, array1) is another very powerful function available in PHP. Basically is a way to remove elements from an array that does not pass given rules in the callable function. These are the most common ways to implement this function:
Option 1. Use a callable outside array_filter function. Declare a function and call it inside the array_filter function:
/**
* @param int $item
* @return bool
*/
function removeOddsFromList(int $item): bool
{
return $item % 2 == 0;
}
$array = [0,1,2,3,4,5,6,7,8,9,10,11,12,3,14,15,56,78,89,45,103,210,211];
$result = array_filter($array, 'removeOddsFromList');
Option 2. Use anonymous function inside the array_filter function:
$array = [0,1,2,3,4,5,6,7,8,9,10,11,12,3,14,15,56,78,89,45,103,210,211];
$result = array_filter($array, function (int $item): bool {return $item % 2 == 0;});
In both cases the output is an associative array corresponding to each one of the elements that remain in the array:
Array
(
[0] => 0
[2] => 2
[4] => 4
[6] => 6
[8] => 8
[10] => 10
[12] => 12
[14] => 14
[16] => 56
[17] => 78
[21] => 210
)
Only the elements that evaluate to true in the call back are returned, in this case, removing all odd numbers. The original array keys are preserved in the result.
Option 3. use array_filter with mode.
$arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6];
$result_1 = array_filter($arr, function($k) {return $k == 'd';}, ARRAY_FILTER_USE_KEY);
$result_2 = array_filter($arr, function($v, $k) {return $k == 'f' || $v == 2;}, ARRAY_FILTER_USE_BOTH);
The output of $result_1
and $result_2
will be:
Array
(
[d] => 4
)
Array
(
[b] => 2
[f] => 6
)
Notice that using
mode = ARRAY_FILTER_USE_KEY
will make the callable to accept the keys of the array while doing
mode = ARRAY_FILTER_USE_BOTH
will make the callable to accept both the key - value pair of each element of the array.
Option 4. apply array_filter without specifying a callback function. This is very useful if we want to remove all empty entries from the array:
$arr = [
0 => 'im not empty',
1 => false,
2 => -1,
3 => null,
4 => '',
5 => '0',
6 => 0,
];
$result = array_filter($arr);
The output will be:
Array
(
[0] => im not empty
[2] => -1
)
In this case, all values that validate to empty (refer to empty function in PHP Documentation) are removed from the result.
For more on this refer to array filter in PHP Documentation.
- Details
- Written by R. Elizondo
- Category: Basics of Software Development
array_column(array, column_name) is another very powerful function available in PHP. It allows to extract all the values from the specified column from a multi-dimensional array without looping into each one of the nested arrays. These are the most common ways to implement this function:
Option 1. With a multi-dimensional array:
$persons = [
[
'id' => 50001,
'first_name' => 'John',
'last_name' => 'Doe',
],
[
'id' => 31245,
'first_name' => 'Sally',
'last_name' => 'Smith',
],
[
'id' => 53642,
'first_name' => 'Jane',
'last_name' => 'Jones',
],
[
'id' => 56213,
'first_name' => 'Peter',
'last_name' => 'Doe',
]
];
$result = array_column($persons, 'last_name');
This will output:
Array
(
[0] => Doe
[1] => Smith
[2] => Jones
[3] => Doe
)
Option 2. Specify a key from the nested arrays as the key for each one of the values extracted:
$persons = [
[
'id' => 50001,
'first_name' => 'John',
'last_name' => 'Doe',
],
[
'id' => 31245,
'first_name' => 'Sally',
'last_name' => 'Smith',
],
[
'id' => 53642,
'first_name' => 'Jane',
'last_name' => 'Jones',
],
[
'id' => 56213,
'first_name' => 'Peter',
'last_name' => 'Doe',
]
];
$result = array_column($persons, 'last_name' , 'id');
This will output:
Array
(
[50001] => Doe
[31245] => Smith
[53642] => Jones
[56213] => Doe
)
Option 3. Get the values of a given property in a array of DTOs (refer to Using DTOs in PHP Symfony, How to validate input data in Symfony using DTOs for more info on these data structures):
class userDto
{
/**
* @var int
*/
public int $id;
/**
* @var string
*/
public string $firstName;
/**
* @var string
*/
public string $lastName;
}
$user_1 = new userDto();
$user_1->id = 101010;
$user_1->firstName ='Sally';
$user_1->lastName = 'Smith';
$user_2 = new userDto();
$user_2->id = 202020;
$user_2->firstName ='John';
$user_2->lastName = 'Doe';
$user_3 = new userDto();
$user_3->id = 303030;
$user_3->firstName ='Peter';
$user_3->lastName = 'Doe';
$persons = [
$user_1,
$user_2,
$user_3
];
$result = array_column($persons, 'firstName' , 'id');
This will output:
Array
(
[101010] => Sally
[202020] => John
[303030] => Peter
)
- Details
- Written by R. Elizondo
- Category: Basics of Software Development
Upgrading old PHP code to the new 8 series could be a very difficult task, but its absolutely necessary. Main reasons are security, performance and compatibility. I will try to document here things I haven't found in other places.
Here are the steps I have been using to upgrade old codebases:
Step 1. Code written in PHP versions older than 5.x upgrade first to PHP 7.4:
- Details
- Written by R. Elizondo
- Category: Symfony Framework
DTOs (Data Transfer Objects) are a special type of objects that are used for carrying over Data between processes. These not only holds the data but the data structure and the data types of each one of the elements of the object. You can use them as a contract between two parties to ensure data consistency and integrity.
If you are working with PHP Symfony Framework and implementing a REST API, you must use DTOs.
I implement DTOs in Symfony this way:
Page 12 of 42