PHP Array_filter on "simple" multi-level array - Stack Overflow
Can someone ELI5 the Javascript array.filter() method?
Is there a PHP equivalent of Javascript's Array.find?
Why don't you make your cities array indexed by id?
More on reddit.comSearching and filtering PHP arrays to later query a database
You have one record per value in your query result, and your array_count_values array gives you the count per record already indexed by your lookup column. Wouldn't something like this give you what you want?
$row = $result->fetch_assoc();More on reddit.com
$row['valueToMultiply'] *= $multiSrvArray['columnName'];
Videos
I think this can not be done using only array_filter function because sometimes you need to modify array elements but the array_filter function allows only to decide if the element should be excluded or not.
For example in the main array element with index 2400 should be included in the result set but it's content should be modified.
I wrote a simple function to do this, hope it might help. Well, you might use this for inspiration. And it was interesting challenge for me as well.
Below is my function with couple tests.
Copy<?php
function deepFilter(array $array)
{
// Formally this is not need because if array is empty then $filteredArray will also be empty
// but it simplifies the algorithm
if (empty($array)) {
return [];
}
$filteredArray = [];
foreach ($array as $key => $value) {
if (is_array($value) && !empty($value)) {
$value = deepFilter($value);
}
if (!empty($value)) {
$filteredArray[$key] = $value;
}
}
return $filteredArray;
}
$testArray1 = [
2400 => [
0 => [
'value' => 7,
0 => 7,
],
1 => [
'value' => 61,
0 => 61,
],
2 => [
'value' => 42,
0 => 42,
],
3 => [
'value' => null,
0 => null,
]
]
];
$testArray2 = [
2400 => [
0 => [
'value' => 7,
0 => 7,
],
1 => [
'value' => 61,
0 => 61,
],
2 => [
'value' => 42,
0 => 42,
],
3 => null
],
3243 => [
0 => [
'value' => 7,
0 => null,
],
1 => [
'value' => null,
0 => 61,
],
2 => [
'value' => 42,
0 => 42,
],
3 => null
]
];
var_export(deepFilter($testArray1));
var_export(deepFilter($testArray2));
The idea is very simple.
- Take an array and check elements one by one.
- If element is an array, apply the function for that element and check the result. We can remove everything from child array and in this case we should not add it to results. Else if child has something remaining after cleanup include 'cleaned child' in our result set.
- If our element is not an array then include it only if it's not empty.
Please let me know if you find any mistakes or if it works for you or not.
Use array_filter and test the value element (or the 0 element, since they're equivalent).
Copy$array[2400] = array_filter($array[2400], function($element) {
return $element['value'];
});
To do it for all elements of the outer array, use a foreach loop.
Copyforeach ($array as &$subarray) {
$subarray = array_filter($subarray, function($element) {
return $element['value'];
});
}
or array_map:
Copy$array = array_map(function($subarray) {
return array_filter($subarray, function($element) {
return $element['value'];
});
}, $array);