Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中对数组进行分组?_Php_Arrays - Fatal编程技术网

如何在PHP中对数组进行分组?

如何在PHP中对数组进行分组?,php,arrays,Php,Arrays,如何按houseid对该阵列进行分组 Array ( [0] => stdClass Object ( [id] => 1111 [houseid] => 58 [price] => 2995 ) [1] => stdClass Object ( [id] => 1112 [h

如何按houseid对该阵列进行分组

Array
(
    [0] => stdClass Object
        (
            [id] => 1111
            [houseid] => 58
            [price] => 2995
        )

    [1] => stdClass Object
        (
            [id] => 1112
            [houseid] => 58
            [price] => 4050
        )

    [2] => stdClass Object
        (
            [id] => 1114
            [houseid] => 60
            [price] => 1695
        )

    [3] => stdClass Object
        (
            [id] => 1115
            [houseid] => 60
            [price] => 2250
        )

    [4] => stdClass Object
        (
            [id] => 1116
            [houseid] => 60
            [price] => 2295
        )
)
我需要计算一个房屋ID的总价格,并做一些数学/条件,如果这个数组中的组

我可以将此数组格式化为类似的格式吗

Array
(
    [58] => stdClass Object(
        [0] => stdClass Object
        (
            [id] => 1111
            [price] => 2995
        )
        [1] => stdClass Object
        (
            [id] => 1112
            [houseid] => 58
            [price] => 4050
        )
    )
    [60] => stdClass Object(
        [0] => stdClass Object(
            [id] => 1114
            [price] => 1695
        )
        [1] => stdClass Object
        (
            [id] => 1115
            [price] => 2250
        )
        [2] => stdClass Object
        (
            [id] => 1116
            [price] => 2295
        )
)

或者有人能建议我,如何循环原始数组来计算房子的总价格吗?

您可以使用
array\u reduce

$data = array_reduce($data, function ($a, $b) {
    $id = $b->houseid;
    unset($b->houseid);
    $a[$id][] = $b;
    return $a;
});

var_dump($data);
输出

array (size=2)
  58 => 
    array (size=2)
      0 => 
        object(stdClass)[1]
          public 'id' => int 1111
          public 'price' => int 2995
      1 => 
        object(stdClass)[2]
          public 'id' => int 1112
          public 'price' => int 4050
  60 => 
    array (size=3)
      0 => 
        object(stdClass)[3]
          public 'id' => int 1114
          public 'price' => int 1695
      1 => 
        object(stdClass)[4]
          public 'id' => int 1115
          public 'price' => int 2250
      2 => 
        object(stdClass)[5]
          public 'id' => int 1116
          public 'price' => int 2295

给我们看看。关于这一点,有很多类似的问答。我需要在一些日期之间获取houseids,并从sql语句中获取此数组……我需要将其重新格式化,以满足需要,因为有很多数组函数,至少你应该在问问题之前检查它们,这表明我花了将近2个小时在谷歌上,检查php笔记等等…这一切都取决于你的处境…当你脑子里有截止日期的时候,有时非常简单的任务似乎也很困难…我真的不知道数组_reduce@luvboy查看文档通常会对我有所帮助,因为它在那里显示了所有可能的
数组
函数,我从来不知道array_reduce,非常感谢爸爸。。。