【问题标题】:php how to count values in multidimensional array from another arrayphp如何从另一个数组计算多维数组中的值
【发布时间】:2023-02-06 23:39:14
【问题描述】:

如何在 $users 数组中计算 name = 'Michael' AND sname = ('Douglas' OR 'Jordan')

$users = array(
    array(
        'id' => '20',
        'name' => 'Michael',
        'sname' => 'Douglas'
    ),
    array(
        'id' => '32',
        'name' => 'Michael',
        'sname' => 'Jackson'
    ),
    array(
        'id' => '56',
        'name' => 'Michael',
        'sname' => 'Jordan'
    )
);
$find = array ( 
    [name] => array ( [0] => 'Michael' ) 
    [sname] => array ( [0] => 'Douglas' [1] =>  'Jordan' ) 
);

现在我只能算在数组“迈克尔”或“道格拉斯”或“乔丹”中

【问题讨论】:

  • 您可能必须基于循环编写一些代码

标签: php arrays multidimensional-array count


【解决方案1】:

在 php https://www.w3schools.com/php/func_array_filter.asp 中尝试数组过滤器

$result = array_filter($users, function($user) {
    return $user['name'] == 'Michael' && in_array($user['sname'], ['Douglas', 'Jordan']);
});

$count = count($result);

【讨论】:

  • 不确定对名称进行硬编码是否是一个好的解决方案。
猜你喜欢
  • 2016-10-13
  • 1970-01-01
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 2013-10-26
相关资源
最近更新 更多