![](/img/trans.png)
[英]Filter Empty/Null value from associative array in php (multidimensional array)
[英]filter multidimensional array by value
我有以下數組:
$entrate=Array
(
0 => Array(
'id' => 1,
'nome' => 'Stipendio',
'datamov' => '2023-04-15',
'mese' => 4,
'anno' => 2023,
'total_mov' => 3000.00
),
1 => Array(
'id' => 1,
'nome' => 'Stipendio',
'datamov' => '2023-03-15',
'mese' => 3,
'anno' => 2023,
'total_mov' => 3000.00
),
2 => Array(
'id' => 1,
'nome' => 'Stipendio',
'datamov' => '2023-02-15',
'mese' => 2,
'anno' => 2023,
'total_mov' => 3000.00
),
3 => Array(
'id' => 1,
'nome' => 'Stipendio',
'datamov' => '2023-01-15',
'mese' => 1,
'anno' => 2023,
'total_mov' => 3000.00
),
4 => Array(
'id' => 4,
'nome' => 'Interessi Attivi',
'datamov' => '',
'mese' => '',
'anno' => '',
'total_mov' => 'VUOTO'
)
);
從這里開始,我希望能夠 select 一個特定'nome'
並過濾數組以僅返回該數組中與該'nome'
匹配的項目。 因此,例如,如果我想按“Stipendio”進行過濾,我將只獲得前三個元素。 如果我只按“Interessi Attivi”過濾第四個。 如果我按“其他鍵”過濾,我將得到一個空數組。
我正在嘗試使用
$tipo_mov='Stipendio';
$entrata = array_filter($entrate, function ($value) use ($tipo_mov) {
return ($entrate[$key]['nome'] == $tipo_mov);
});
但我無法調整它,使其返回預期的過濾(這段代碼顯然沒有運行並拋出一堆錯誤)。
我錯過了什么?
array_filter()
中的$value
是$entrate
的內部 arrays,因此您要在$value['nome']
上搜索匹配項:
https://paiza.io/projects/BmEdekWY8elJ90rIPjTGGQ
$tipo_mov='Stipendio';
$entrata = array_filter($entrate, function ($value) use ($tipo_mov)
{
return $value['nome'] == $tipo_mov;
});
print_r($entrata);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.