Php 根据数组字段值将大数组拆分为多个数组

Php 根据数组字段值将大数组拆分为多个数组,php,arrays,Php,Arrays,我有一个数组,它很大。非常大。这是给一家房地产公司的。我想根据每个家庭拥有的卧室数量将阵列分成若干部分。因此,理想情况下,所有具有0个卧室的条目(如下面的条目)都将被设置到一个数组中,而具有1的条目将被设置到另一个数组中。等等 这可能吗 object(SimpleXMLElement)#124 (1) { ["unit"]=> array(40) { [0]=> object(SimpleXMLElement)#246 (1) { ["@attri

我有一个数组,它很大。非常大。这是给一家房地产公司的。我想根据每个家庭拥有的卧室数量将阵列分成若干部分。因此,理想情况下,所有具有0个卧室的条目(如下面的条目)都将被设置到一个数组中,而具有1的条目将被设置到另一个数组中。等等

这可能吗

object(SimpleXMLElement)#124 (1) {
  ["unit"]=>
  array(40) {
    [0]=>
    object(SimpleXMLElement)#246 (1) {
      ["@attributes"]=>          array(28) {
        ["mlsnumber"]=>            string(8) "A1837040"
        ["type"]=>            string(0) ""
        ["unit"]=>            string(3) "607"
        ["maintenancefee"]=>            string(4) "1609"
        ["price"]=>            string(6) "890000"
        ["salesprice"]=>            string(0) ""
        ["bath"]=>            string(1) "1"
        ["hbath"]=>            string(1) "0"
        ["beds"]=>            string(1) "0"
        ["sqft"]=>            string(3) "747"
        ["sqftm"]=>            string(5) "69.40"
        ["pricesqft"]=>            string(8) "1,191.43"
        ["lat"]=>            string(16) "25.7683201213024"
        ["lng"]=>            string(16) "-80.132474899292"
        ["pricemeters"]=>            string(9) "12,824.21"
        ["pricechange"]=>            string(5) "-6.32"
        ["dom"]=>            string(3) "181"
        ["reo"]=>            string(1) "N"
        ["shortsale"]=>            string(1) "N"
        ["dropprice"]=>            string(0) ""
        ["pets"]=>            string(3) "Yes"
        ["furnished"]=>            string(1) "U"
        ["FloorLevel"]=>            string(1) "6"
      }
    }

循环遍历阵列并将当前阵列推入新阵列,而新阵列的索引是当前阵列的卧室数。您需要首先将对象转换为数组,然后按照以下方式进行转换:

  foreach($all_units as $unit){
    if(!isset($new[$unit['beds']])){
          $new[$unit['beds']] = array();
    }
    array_push($new[$unit['beds']],$unit);
  }
  print_r($new);

假设
$arr
保存您的结果,请重试

$result = array();
foreach($arr['unit'] as $prop){
  $result[$prop["beds"]][] = $prop;
}

所有阵列都在单元内吗?是的,它们都在单元内我有6套床位计数。1,2,3,4,5,7. 这个代码看起来只会推出零床的数组。为什么?这个isset只是避免警告。如果$unit[beds]包含卧室数,则最终会得到一个多维数组$new,该数组按卧室数进行索引,即$unit[2]包含两个卧室的数组。我确实尝试过,它会返回解析错误:语法错误,意外“,”,预期“]”我不知道,仍然不起作用。我喜欢这个代码的工作。警告:偏移量类型非法